cg386mem.pas 37 KB

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