cg386mem.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 assembler for in memory related nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cg386mem;
  19. interface
  20. uses
  21. tree;
  22. procedure secondloadvmt(var p : ptree);
  23. procedure secondhnewn(var p : ptree);
  24. procedure secondnewn(var p : ptree);
  25. procedure secondhdisposen(var p : ptree);
  26. procedure secondsimplenewdispose(var p : ptree);
  27. procedure secondaddr(var p : ptree);
  28. procedure seconddoubleaddr(var p : ptree);
  29. procedure secondderef(var p : ptree);
  30. procedure secondsubscriptn(var p : ptree);
  31. procedure secondvecn(var p : ptree);
  32. procedure secondselfn(var p : ptree);
  33. procedure secondwith(var p : ptree);
  34. implementation
  35. uses
  36. globtype,systems,
  37. cobjects,verbose,globals,
  38. symtable,aasm,types,
  39. hcodegen,temp_gen,pass_2,
  40. {$ifdef ag386bin}
  41. i386base,i386asm,
  42. {$else}
  43. i386,
  44. {$endif}
  45. cgai386,tgeni386;
  46. {*****************************************************************************
  47. SecondLoadVMT
  48. *****************************************************************************}
  49. procedure secondloadvmt(var p : ptree);
  50. begin
  51. p^.location.register:=getregister32;
  52. exprasmlist^.concat(new(pai386,op_sym_ofs_reg(A_MOV,
  53. S_L,newasmsymbol(pobjectdef(pclassrefdef(p^.resulttype)^.definition)^.vmt_mangledname),0,
  54. p^.location.register)));
  55. maybe_concat_external(pobjectdef(pclassrefdef(p^.resulttype)^.definition)^.owner,
  56. pobjectdef(pclassrefdef(p^.resulttype)^.definition)^.vmt_mangledname);
  57. end;
  58. {*****************************************************************************
  59. SecondHNewN
  60. *****************************************************************************}
  61. procedure secondhnewn(var p : ptree);
  62. begin
  63. end;
  64. {*****************************************************************************
  65. SecondNewN
  66. *****************************************************************************}
  67. procedure secondnewn(var p : ptree);
  68. var
  69. pushed : tpushed;
  70. r : preference;
  71. begin
  72. if assigned(p^.left) then
  73. begin
  74. secondpass(p^.left);
  75. p^.location.register:=p^.left^.location.register;
  76. end
  77. else
  78. begin
  79. pushusedregisters(pushed,$ff);
  80. { code copied from simplenewdispose PM }
  81. { determines the size of the mem block }
  82. push_int(ppointerdef(p^.resulttype)^.definition^.size);
  83. gettempofsizereference(target_os.size_of_pointer,p^.location.reference);
  84. emitpushreferenceaddr(exprasmlist,p^.location.reference);
  85. emitcall('FPC_GETMEM',true);
  86. if ppointerdef(p^.resulttype)^.definition^.needs_inittable then
  87. begin
  88. new(r);
  89. reset_reference(r^);
  90. r^.symbol:=newasmsymbol(lab2str(ppointerdef(p^.left^.resulttype)^.definition^.get_inittable_label));
  91. emitpushreferenceaddr(exprasmlist,r^);
  92. { push pointer adress }
  93. emitpushreferenceaddr(exprasmlist,p^.location.reference);
  94. dispose(r);
  95. emitcall('FPC_INITIALIZE',true);
  96. end;
  97. popusedregisters(pushed);
  98. { may be load ESI }
  99. maybe_loadesi;
  100. end;
  101. if codegenerror then
  102. exit;
  103. end;
  104. {*****************************************************************************
  105. SecondDisposeN
  106. *****************************************************************************}
  107. procedure secondhdisposen(var p : ptree);
  108. begin
  109. secondpass(p^.left);
  110. if codegenerror then
  111. exit;
  112. clear_reference(p^.location.reference);
  113. case p^.left^.location.loc of
  114. LOC_REGISTER,
  115. LOC_CREGISTER:
  116. begin
  117. p^.location.reference.index:=getregister32;
  118. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  119. p^.left^.location.register,
  120. p^.location.reference.index)));
  121. end;
  122. LOC_MEM,LOC_REFERENCE :
  123. begin
  124. del_reference(p^.left^.location.reference);
  125. p^.location.reference.index:=getregister32;
  126. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  127. p^.location.reference.index)));
  128. end;
  129. end;
  130. end;
  131. {*****************************************************************************
  132. SecondNewDispose
  133. *****************************************************************************}
  134. procedure secondsimplenewdispose(var p : ptree);
  135. var
  136. pushed : tpushed;
  137. r : preference;
  138. begin
  139. secondpass(p^.left);
  140. if codegenerror then
  141. exit;
  142. pushusedregisters(pushed,$ff);
  143. { determines the size of the mem block }
  144. push_int(ppointerdef(p^.left^.resulttype)^.definition^.size);
  145. { push pointer adress }
  146. case p^.left^.location.loc of
  147. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,
  148. p^.left^.location.register)));
  149. LOC_REFERENCE:
  150. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  151. end;
  152. { call the mem handling procedures }
  153. case p^.treetype of
  154. simpledisposen:
  155. begin
  156. if ppointerdef(p^.left^.resulttype)^.definition^.needs_inittable then
  157. begin
  158. new(r);
  159. reset_reference(r^);
  160. r^.symbol:=newasmsymbol(lab2str(ppointerdef(p^.left^.resulttype)^.definition^.get_inittable_label));
  161. emitpushreferenceaddr(exprasmlist,r^);
  162. { push pointer adress }
  163. case p^.left^.location.loc of
  164. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,
  165. p^.left^.location.register)));
  166. LOC_REFERENCE:
  167. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  168. end;
  169. dispose(r);
  170. emitcall('FPC_FINALIZE',true);
  171. end;
  172. emitcall('FPC_FREEMEM',true);
  173. end;
  174. simplenewn:
  175. begin
  176. emitcall('FPC_GETMEM',true);
  177. if ppointerdef(p^.left^.resulttype)^.definition^.needs_inittable then
  178. begin
  179. new(r);
  180. reset_reference(r^);
  181. r^.symbol:=newasmsymbol(lab2str(ppointerdef(p^.left^.resulttype)^.definition^.get_inittable_label));
  182. emitpushreferenceaddr(exprasmlist,r^);
  183. { push pointer adress }
  184. case p^.left^.location.loc of
  185. LOC_CREGISTER : exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,
  186. p^.left^.location.register)));
  187. LOC_REFERENCE:
  188. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  189. end;
  190. dispose(r);
  191. emitcall('FPC_INITIALIZE',true);
  192. end;
  193. end;
  194. end;
  195. popusedregisters(pushed);
  196. { may be load ESI }
  197. maybe_loadesi;
  198. end;
  199. {*****************************************************************************
  200. SecondAddr
  201. *****************************************************************************}
  202. procedure secondaddr(var p : ptree);
  203. begin
  204. secondpass(p^.left);
  205. p^.location.loc:=LOC_REGISTER;
  206. del_reference(p^.left^.location.reference);
  207. p^.location.register:=getregister32;
  208. {@ on a procvar means returning an address to the procedure that
  209. is stored in it.}
  210. { yes but p^.left^.symtableentry can be nil
  211. for example on @self !! }
  212. { symtableentry can be also invalid, if left is no tree node }
  213. if (m_tp_procvar in aktmodeswitches) and
  214. (p^.left^.treetype=loadn) and
  215. assigned(p^.left^.symtableentry) and
  216. (p^.left^.symtableentry^.typ=varsym) and
  217. (pvarsym(p^.left^.symtableentry)^.definition^.deftype=procvardef) then
  218. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  219. newreference(p^.left^.location.reference),
  220. p^.location.register)))
  221. else
  222. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  223. newreference(p^.left^.location.reference),
  224. p^.location.register)));
  225. { for use of other segments }
  226. if p^.left^.location.reference.segment<>R_DEFAULT_SEG then
  227. p^.location.segment:=p^.left^.location.reference.segment;
  228. end;
  229. {*****************************************************************************
  230. SecondDoubleAddr
  231. *****************************************************************************}
  232. procedure seconddoubleaddr(var p : ptree);
  233. begin
  234. secondpass(p^.left);
  235. p^.location.loc:=LOC_REGISTER;
  236. del_reference(p^.left^.location.reference);
  237. p^.location.register:=getregister32;
  238. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  239. newreference(p^.left^.location.reference),
  240. p^.location.register)));
  241. end;
  242. {*****************************************************************************
  243. SecondDeRef
  244. *****************************************************************************}
  245. procedure secondderef(var p : ptree);
  246. var
  247. hr : tregister;
  248. begin
  249. secondpass(p^.left);
  250. clear_reference(p^.location.reference);
  251. case p^.left^.location.loc of
  252. LOC_REGISTER:
  253. p^.location.reference.base:=p^.left^.location.register;
  254. LOC_CREGISTER:
  255. begin
  256. { ... and reserve one for the pointer }
  257. hr:=getregister32;
  258. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hr);
  259. p^.location.reference.base:=hr;
  260. end;
  261. else
  262. begin
  263. { free register }
  264. del_reference(p^.left^.location.reference);
  265. { ...and reserve one for the pointer }
  266. hr:=getregister32;
  267. exprasmlist^.concat(new(pai386,op_ref_reg(
  268. A_MOV,S_L,newreference(p^.left^.location.reference),
  269. hr)));
  270. p^.location.reference.base:=hr;
  271. end;
  272. end;
  273. if p^.left^.resulttype^.deftype=farpointerdef then
  274. p^.location.reference.segment:=R_FS;
  275. end;
  276. {*****************************************************************************
  277. SecondSubScriptN
  278. *****************************************************************************}
  279. procedure secondsubscriptn(var p : ptree);
  280. var
  281. hr : tregister;
  282. begin
  283. secondpass(p^.left);
  284. if codegenerror then
  285. exit;
  286. { classes must be dereferenced implicit }
  287. if (p^.left^.resulttype^.deftype=objectdef) and
  288. pobjectdef(p^.left^.resulttype)^.isclass then
  289. begin
  290. clear_reference(p^.location.reference);
  291. case p^.left^.location.loc of
  292. LOC_REGISTER:
  293. p^.location.reference.base:=p^.left^.location.register;
  294. LOC_CREGISTER:
  295. begin
  296. { ... and reserve one for the pointer }
  297. hr:=getregister32;
  298. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hr);
  299. p^.location.reference.base:=hr;
  300. end;
  301. else
  302. begin
  303. { free register }
  304. del_reference(p^.left^.location.reference);
  305. { ... and reserve one for the pointer }
  306. hr:=getregister32;
  307. exprasmlist^.concat(new(pai386,op_ref_reg(
  308. A_MOV,S_L,newreference(p^.left^.location.reference),
  309. hr)));
  310. p^.location.reference.base:=hr;
  311. end;
  312. end;
  313. end
  314. else
  315. set_location(p^.location,p^.left^.location);
  316. inc(p^.location.reference.offset,p^.vs^.address);
  317. end;
  318. {*****************************************************************************
  319. SecondVecN
  320. *****************************************************************************}
  321. procedure secondvecn(var p : ptree);
  322. var
  323. is_pushed : boolean;
  324. ind,hr : tregister;
  325. _p : ptree;
  326. function get_mul_size:longint;
  327. begin
  328. if p^.memindex then
  329. get_mul_size:=1
  330. else
  331. get_mul_size:=p^.resulttype^.size;
  332. end;
  333. procedure calc_emit_mul;
  334. var
  335. l1,l2 : longint;
  336. begin
  337. l1:=get_mul_size;
  338. case l1 of
  339. 1,2,4,8 : p^.location.reference.scalefactor:=l1;
  340. else
  341. begin
  342. if ispowerof2(l1,l2) then
  343. exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,l2,ind)))
  344. else
  345. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,S_L,l1,ind)));
  346. end;
  347. end;
  348. end;
  349. var
  350. extraoffset : longint;
  351. { rl stores the resulttype of the left node, this is necessary }
  352. { to detect if it is an ansistring }
  353. { because in constant nodes which constant index }
  354. { the left tree is removed }
  355. rl : pdef;
  356. t : ptree;
  357. hp : preference;
  358. tai : Pai386;
  359. pushed : tpushed;
  360. begin
  361. secondpass(p^.left);
  362. rl:=p^.left^.resulttype;
  363. { we load the array reference to p^.location }
  364. { an ansistring needs to be dereferenced }
  365. if is_ansistring(p^.left^.resulttype) or
  366. is_widestring(p^.left^.resulttype) then
  367. begin
  368. reset_reference(p^.location.reference);
  369. if p^.callunique then
  370. begin
  371. if p^.left^.location.loc<>LOC_REFERENCE then
  372. begin
  373. CGMessage(cg_e_illegal_expression);
  374. exit;
  375. end;
  376. pushusedregisters(pushed,$ff);
  377. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  378. if is_ansistring(p^.left^.resulttype) then
  379. emitcall('FPC_ANSISTR_UNIQUE',true)
  380. else
  381. emitcall('FPC_WIDESTR_UNIQUE',true);
  382. maybe_loadesi;
  383. popusedregisters(pushed);
  384. end;
  385. if p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  386. begin
  387. p^.location.reference.base:=p^.left^.location.register;
  388. end
  389. else
  390. begin
  391. del_reference(p^.left^.location.reference);
  392. p^.location.reference.base:=getregister32;
  393. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  394. newreference(p^.left^.location.reference),
  395. p^.location.reference.base)));
  396. end;
  397. { check for a zero length string,
  398. we can use the ansistring routine here }
  399. if (cs_check_range in aktlocalswitches) then
  400. begin
  401. pushusedregisters(pushed,$ff);
  402. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.location.reference.base)));
  403. emitcall('FPC_ANSISTR_CHECKZERO',true);
  404. maybe_loadesi;
  405. popusedregisters(pushed);
  406. end;
  407. if is_ansistring(p^.left^.resulttype) then
  408. { in ansistrings S[1] is pchar(S)[0] !! }
  409. dec(p^.location.reference.offset)
  410. else
  411. begin
  412. { in widestrings S[1] is pwchar(S)[0] !! }
  413. dec(p^.location.reference.offset,2);
  414. exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,
  415. 1,p^.location.reference.base)));
  416. end;
  417. { we've also to keep left up-to-date, because it is used }
  418. { if a constant array index occurs, subject to change (FK) }
  419. set_location(p^.left^.location,p^.location);
  420. end
  421. else
  422. set_location(p^.location,p^.left^.location);
  423. { offset can only differ from 0 if arraydef }
  424. if p^.left^.resulttype^.deftype=arraydef then
  425. dec(p^.location.reference.offset,
  426. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  427. if p^.right^.treetype=ordconstn then
  428. begin
  429. { offset can only differ from 0 if arraydef }
  430. if (p^.left^.resulttype^.deftype=arraydef) then
  431. begin
  432. if not(is_open_array(p^.left^.resulttype)) then
  433. begin
  434. if (p^.right^.value>parraydef(p^.left^.resulttype)^.highrange) or
  435. (p^.right^.value<parraydef(p^.left^.resulttype)^.lowrange) then
  436. begin
  437. if (cs_check_range in aktlocalswitches) then
  438. CGMessage(parser_e_range_check_error)
  439. else
  440. CGMessage(parser_w_range_check_error);
  441. end;
  442. dec(p^.left^.location.reference.offset,
  443. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  444. end
  445. else
  446. begin
  447. { range checking for open arrays !!!! }
  448. {!!!!!!!!!!!!!!!!!}
  449. end;
  450. end
  451. else if (p^.left^.resulttype^.deftype=stringdef) then
  452. begin
  453. if (p^.right^.value=0) and not(is_shortstring(p^.left^.resulttype)) then
  454. CGMessage(cg_e_can_access_element_zero);
  455. if (cs_check_range in aktlocalswitches) then
  456. case pstringdef(p^.left^.resulttype)^.string_typ of
  457. { it's the same for ansi- and wide strings }
  458. st_widestring,
  459. st_ansistring:
  460. begin
  461. pushusedregisters(pushed,$ff);
  462. push_int(p^.right^.value);
  463. hp:=newreference(p^.location.reference);
  464. dec(hp^.offset,7);
  465. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,hp)));
  466. emitcall('FPC_ANSISTR_RANGECHECK',true);
  467. popusedregisters(pushed);
  468. maybe_loadesi;
  469. end;
  470. st_shortstring:
  471. begin
  472. {!!!!!!!!!!!!!!!!!}
  473. end;
  474. st_longstring:
  475. begin
  476. {!!!!!!!!!!!!!!!!!}
  477. end;
  478. end;
  479. end;
  480. inc(p^.left^.location.reference.offset,
  481. get_mul_size*p^.right^.value);
  482. if p^.memseg then
  483. p^.left^.location.reference.segment:=R_FS;
  484. p^.left^.resulttype:=p^.resulttype;
  485. disposetree(p^.right);
  486. _p:=p^.left;
  487. putnode(p);
  488. p:=_p;
  489. end
  490. else
  491. begin
  492. { quick hack, to overcome Delphi 2 }
  493. if (cs_regalloc in aktglobalswitches) and
  494. (p^.left^.resulttype^.deftype=arraydef) then
  495. begin
  496. extraoffset:=0;
  497. if (p^.right^.treetype=addn) then
  498. begin
  499. if p^.right^.right^.treetype=ordconstn then
  500. begin
  501. extraoffset:=p^.right^.right^.value;
  502. t:=p^.right^.left;
  503. putnode(p^.right);
  504. putnode(p^.right^.right);
  505. p^.right:=t
  506. end
  507. else if p^.right^.left^.treetype=ordconstn then
  508. begin
  509. extraoffset:=p^.right^.left^.value;
  510. t:=p^.right^.right;
  511. putnode(p^.right);
  512. putnode(p^.right^.left);
  513. p^.right:=t
  514. end;
  515. end
  516. else if (p^.right^.treetype=subn) then
  517. begin
  518. if p^.right^.right^.treetype=ordconstn then
  519. begin
  520. extraoffset:=p^.right^.right^.value;
  521. t:=p^.right^.left;
  522. putnode(p^.right);
  523. putnode(p^.right^.right);
  524. p^.right:=t
  525. end
  526. else if p^.right^.left^.treetype=ordconstn then
  527. begin
  528. extraoffset:=p^.right^.left^.value;
  529. t:=p^.right^.right;
  530. putnode(p^.right);
  531. putnode(p^.right^.left);
  532. p^.right:=t
  533. end;
  534. end;
  535. inc(p^.location.reference.offset,
  536. get_mul_size*extraoffset);
  537. end;
  538. { calculate from left to right }
  539. if (p^.location.loc<>LOC_REFERENCE) and
  540. (p^.location.loc<>LOC_MEM) then
  541. CGMessage(cg_e_illegal_expression);
  542. is_pushed:=maybe_push(p^.right^.registers32,p);
  543. secondpass(p^.right);
  544. if is_pushed then restore(p);
  545. case p^.right^.location.loc of
  546. LOC_REGISTER:
  547. begin
  548. ind:=p^.right^.location.register;
  549. case p^.right^.resulttype^.size of
  550. 1:
  551. begin
  552. hr:=reg8toreg32(ind);
  553. emit_reg_reg(A_MOVZX,S_BL,ind,hr);
  554. ind:=hr;
  555. end;
  556. 2:
  557. begin
  558. hr:=reg16toreg32(ind);
  559. emit_reg_reg(A_MOVZX,S_WL,ind,hr);
  560. ind:=hr;
  561. end;
  562. end;
  563. end;
  564. LOC_CREGISTER:
  565. begin
  566. ind:=getregister32;
  567. case p^.right^.resulttype^.size of
  568. 1:
  569. emit_reg_reg(A_MOVZX,S_BL,p^.right^.location.register,ind);
  570. 2:
  571. emit_reg_reg(A_MOVZX,S_WL,p^.right^.location.register,ind);
  572. 4:
  573. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,ind);
  574. end;
  575. end;
  576. LOC_FLAGS:
  577. begin
  578. ind:=getregister32;
  579. emit_flag2reg(p^.right^.location.resflags,reg32toreg8(ind));
  580. emit_reg_reg(A_MOVZX,S_BL,reg32toreg8(ind),ind);
  581. end
  582. else
  583. begin
  584. del_reference(p^.right^.location.reference);
  585. ind:=getregister32;
  586. { Booleans are stored in an 8 bit memory location, so
  587. the use of MOVL is not correct }
  588. case p^.right^.resulttype^.size of
  589. 1:
  590. tai:=new(pai386,op_ref_reg(A_MOVZX,S_BL,newreference(p^.right^.location.reference),ind));
  591. 2:
  592. tai:=new(Pai386,op_ref_reg(A_MOVZX,S_WL,newreference(p^.right^.location.reference),ind));
  593. 4:
  594. tai:=new(Pai386,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),ind));
  595. end;
  596. exprasmlist^.concat(tai);
  597. end;
  598. end;
  599. { produce possible range check code: }
  600. if cs_check_range in aktlocalswitches then
  601. begin
  602. if p^.left^.resulttype^.deftype=arraydef then
  603. begin
  604. hp:=new_reference(R_NO,0);
  605. parraydef(p^.left^.resulttype)^.genrangecheck;
  606. hp^.symbol:=newasmsymbol(parraydef(p^.left^.resulttype)^.getrangecheckstring);
  607. exprasmlist^.concat(new(pai386,op_reg_ref(A_BOUND,S_L,ind,hp)));
  608. end
  609. else if (p^.left^.resulttype^.deftype=stringdef) then
  610. begin
  611. case pstringdef(p^.left^.resulttype)^.string_typ of
  612. { it's the same for ansi- and wide strings }
  613. st_widestring,
  614. st_ansistring:
  615. begin
  616. pushusedregisters(pushed,$ff);
  617. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,ind)));
  618. hp:=newreference(p^.location.reference);
  619. dec(hp^.offset,7);
  620. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,hp)));
  621. emitcall('FPC_ANSISTR_RANGECHECK',true);
  622. popusedregisters(pushed);
  623. maybe_loadesi;
  624. end;
  625. st_shortstring:
  626. begin
  627. {!!!!!!!!!!!!!!!!!}
  628. end;
  629. st_longstring:
  630. begin
  631. {!!!!!!!!!!!!!!!!!}
  632. end;
  633. end;
  634. end;
  635. end;
  636. if p^.location.reference.index=R_NO then
  637. begin
  638. p^.location.reference.index:=ind;
  639. calc_emit_mul;
  640. end
  641. else
  642. begin
  643. if p^.location.reference.base=R_NO then
  644. begin
  645. case p^.location.reference.scalefactor of
  646. 2 : exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,1,p^.location.reference.index)));
  647. 4 : exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,2,p^.location.reference.index)));
  648. 8 : exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,3,p^.location.reference.index)));
  649. end;
  650. calc_emit_mul;
  651. p^.location.reference.base:=p^.location.reference.index;
  652. p^.location.reference.index:=ind;
  653. end
  654. else
  655. begin
  656. exprasmlist^.concat(new(pai386,op_ref_reg(
  657. A_LEA,S_L,newreference(p^.location.reference),
  658. p^.location.reference.index)));
  659. ungetregister32(p^.location.reference.base);
  660. { the symbol offset is loaded, }
  661. { so release the symbol name and set symbol }
  662. { to nil }
  663. p^.location.reference.symbol:=nil;
  664. p^.location.reference.offset:=0;
  665. calc_emit_mul;
  666. p^.location.reference.base:=p^.location.reference.index;
  667. p^.location.reference.index:=ind;
  668. end;
  669. end;
  670. if p^.memseg then
  671. p^.location.reference.segment:=R_FS;
  672. end;
  673. { have we to remove a temp. wide/ansistring ?
  674. c:=(s1+s2)[i]
  675. for example
  676. }
  677. if (p^.location.loc=LOC_MEM) and
  678. (rl^.deftype=stringdef) then
  679. begin
  680. case pstringdef(rl)^.string_typ of
  681. st_ansistring:
  682. begin
  683. del_reference(p^.location.reference);
  684. hr:=reg32toreg8(getregister32);
  685. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_B,
  686. newreference(p^.location.reference),hr)));
  687. clear_reference(p^.location.reference);
  688. p^.location.loc:=LOC_REGISTER;
  689. p^.location.register:=hr;
  690. { we can remove all temps }
  691. removetemps(exprasmlist,temptoremove);
  692. temptoremove^.clear;
  693. end;
  694. st_widestring:
  695. begin
  696. del_reference(p^.location.reference);
  697. hr:=reg32toreg16(getregister32);
  698. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,
  699. newreference(p^.location.reference),hr))); clear_reference(p^.location.reference);
  700. p^.location.loc:=LOC_REGISTER;
  701. p^.location.register:=hr;
  702. { we can remove all temps }
  703. removetemps(exprasmlist,temptoremove);
  704. temptoremove^.clear;
  705. end;
  706. end;
  707. end;
  708. end;
  709. {*****************************************************************************
  710. SecondSelfN
  711. *****************************************************************************}
  712. procedure secondselfn(var p : ptree);
  713. begin
  714. clear_reference(p^.location.reference);
  715. if (p^.resulttype^.deftype=classrefdef) or
  716. ((p^.resulttype^.deftype=objectdef)
  717. and pobjectdef(p^.resulttype)^.isclass
  718. ) then
  719. p^.location.register:=R_ESI
  720. else
  721. p^.location.reference.base:=R_ESI;
  722. end;
  723. {*****************************************************************************
  724. SecondWithN
  725. *****************************************************************************}
  726. procedure secondwith(var p : ptree);
  727. var
  728. ref : treference;
  729. symtable : psymtable;
  730. i : longint;
  731. load : boolean;
  732. begin
  733. if assigned(p^.left) then
  734. begin
  735. secondpass(p^.left);
  736. load:=true;
  737. if p^.left^.location.reference.segment<>R_DEFAULT_SEG then
  738. message(parser_e_no_with_for_variable_in_other_segments);
  739. ref.symbol:=nil;
  740. gettempofsizereference(4,ref);
  741. if (p^.left^.treetype=loadn) and
  742. (p^.left^.symtable=aktprocsym^.definition^.localst) then
  743. begin
  744. { for local class just use the local storage }
  745. ungetiftemp(ref);
  746. new(p^.pref);
  747. p^.pref^:=p^.left^.location.reference;
  748. { don't discard symbol if in main procedure }
  749. p^.left^.location.reference.symbol:=nil;
  750. load:=false;
  751. end
  752. else if (p^.left^.resulttype^.deftype=objectdef) and
  753. pobjectdef(p^.left^.resulttype)^.isclass then
  754. begin
  755. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  756. newreference(p^.left^.location.reference),R_EDI)))
  757. end
  758. else
  759. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  760. newreference(p^.left^.location.reference),R_EDI)));
  761. if load then
  762. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  763. R_EDI,newreference(ref))));
  764. del_reference(p^.left^.location.reference);
  765. { the offset relative to (%ebp) is only needed here! }
  766. symtable:=p^.withsymtable;
  767. for i:=1 to p^.tablecount do
  768. begin
  769. symtable^.datasize:=ref.offset;
  770. symtable:=symtable^.next;
  771. end;
  772. { p^.right can be optimize out !!! }
  773. if p^.right<>nil then
  774. secondpass(p^.right);
  775. { clear some stuff }
  776. if assigned(p^.pref) then
  777. dispose(p^.pref);
  778. if load then
  779. ungetiftemp(ref);
  780. end;
  781. end;
  782. end.
  783. {
  784. $Log$
  785. Revision 1.31 1999-02-25 21:02:29 peter
  786. * ag386bin updates
  787. + coff writer
  788. Revision 1.30 1999/02/22 02:15:14 peter
  789. * updates for ag386bin
  790. Revision 1.29 1999/02/07 22:53:07 florian
  791. * potential bug in secondvecn fixed
  792. Revision 1.28 1999/02/04 17:16:51 peter
  793. * fixed crash with temp ansistring indexing
  794. Revision 1.27 1999/02/04 11:44:46 florian
  795. * fixed indexed access of ansistrings to temp. ansistring, i.e.
  796. c:=(s1+s2)[i], the temp is now correctly remove and the generated
  797. code is also fixed
  798. Revision 1.26 1999/02/04 10:49:41 florian
  799. + range checking for ansi- and widestrings
  800. * made it compilable with TP
  801. Revision 1.25 1999/01/21 16:40:52 pierre
  802. * fix for constructor inside with statements
  803. Revision 1.24 1999/01/19 12:05:27 pierre
  804. * bug with @procvar=procvar fiwed
  805. Revision 1.23 1998/12/30 22:15:45 peter
  806. + farpointer type
  807. * absolutesym now also stores if its far
  808. Revision 1.22 1998/12/11 00:02:55 peter
  809. + globtype,tokens,version unit splitted from globals
  810. Revision 1.21 1998/12/10 09:47:18 florian
  811. + basic operations with int64/qord (compiler with -dint64)
  812. + rtti of enumerations extended: names are now written
  813. Revision 1.20 1998/11/25 19:12:54 pierre
  814. * var:=new(pointer_type) support added
  815. Revision 1.19 1998/11/20 15:35:55 florian
  816. * problems with rtti fixed, hope it works
  817. Revision 1.18 1998/11/17 00:36:40 peter
  818. * more ansistring fixes
  819. Revision 1.17 1998/11/16 15:35:09 pierre
  820. * added error for with if different segment
  821. Revision 1.16 1998/10/21 11:44:42 florian
  822. + check for access to index 0 of long/wide/ansi strings added,
  823. gives now an error
  824. * problem with access to contant index of ansistrings fixed
  825. Revision 1.15 1998/10/12 09:49:53 florian
  826. + support of <procedure var type>:=<pointer> in delphi mode added
  827. Revision 1.14 1998/10/02 07:20:37 florian
  828. * range checking in units doesn't work if the units are smartlinked, fixed
  829. Revision 1.13 1998/09/27 10:16:23 florian
  830. * type casts pchar<->ansistring fixed
  831. * ansistring[..] calls does now an unique call
  832. Revision 1.12 1998/09/23 15:46:36 florian
  833. * problem with with and classes fixed
  834. Revision 1.11 1998/09/17 09:42:18 peter
  835. + pass_2 for cg386
  836. * Message() -> CGMessage() for pass_1/pass_2
  837. Revision 1.10 1998/09/14 10:43:52 peter
  838. * all internal RTL functions start with FPC_
  839. Revision 1.9 1998/09/03 16:03:15 florian
  840. + rtti generation
  841. * init table generation changed
  842. Revision 1.8 1998/08/23 21:04:34 florian
  843. + rtti generation for classes added
  844. + new/dispose do now also a call to INITIALIZE/FINALIZE, if necessaray
  845. Revision 1.7 1998/08/20 11:27:40 michael
  846. * Applied Peters Fix
  847. Revision 1.6 1998/08/10 14:49:49 peter
  848. + localswitches, moduleswitches, globalswitches splitting
  849. Revision 1.5 1998/07/26 21:58:58 florian
  850. + better support for switch $H
  851. + index access to ansi strings added
  852. + assigment of data (records/arrays) containing ansi strings
  853. Revision 1.4 1998/07/24 22:16:55 florian
  854. * internal error 10 together with array access fixed. I hope
  855. that's the final fix.
  856. Revision 1.3 1998/06/25 08:48:09 florian
  857. * first version of rtti support
  858. Revision 1.2 1998/06/08 13:13:35 pierre
  859. + temporary variables now in temp_gen.pas unit
  860. because it is processor independent
  861. * mppc68k.bat modified to undefine i386 and support_mmx
  862. (which are defaults for i386)
  863. Revision 1.1 1998/06/05 17:44:13 peter
  864. * splitted cgi386
  865. }