cg386mem.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. t : ptree;
  348. hp : preference;
  349. tai : Pai386;
  350. pushed : tpushed;
  351. begin
  352. secondpass(p^.left);
  353. { we load the array reference to p^.location }
  354. { an ansistring needs to be dereferenced }
  355. if is_ansistring(p^.left^.resulttype) or
  356. is_widestring(p^.left^.resulttype) then
  357. begin
  358. reset_reference(p^.location.reference);
  359. if p^.callunique then
  360. begin
  361. if p^.left^.location.loc<>LOC_REFERENCE then
  362. begin
  363. CGMessage(cg_e_illegal_expression);
  364. exit;
  365. end;
  366. pushusedregisters(pushed,$ff);
  367. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  368. if is_ansistring(p^.left^.resulttype) then
  369. emitcall('FPC_ANSISTR_UNIQUE',true)
  370. else
  371. emitcall('FPC_WIDESTR_UNIQUE',true);
  372. maybe_loadesi;
  373. popusedregisters(pushed);
  374. end;
  375. if p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  376. begin
  377. p^.location.reference.base:=p^.left^.location.register;
  378. end
  379. else
  380. begin
  381. del_reference(p^.left^.location.reference);
  382. p^.location.reference.base:=getregister32;
  383. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  384. newreference(p^.left^.location.reference),
  385. p^.location.reference.base)));
  386. end;
  387. { check for a zero length string,
  388. we can use the ansistring routine here }
  389. if (cs_check_range in aktlocalswitches) then
  390. begin
  391. pushusedregisters(pushed,$ff);
  392. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.location.reference.base)));
  393. emitcall('FPC_ANSISTR_CHECKZERO',true);
  394. maybe_loadesi;
  395. popusedregisters(pushed);
  396. end;
  397. if is_ansistring(p^.left^.resulttype) then
  398. { in ansistrings S[1] is pchar(S)[0] !! }
  399. dec(p^.location.reference.offset)
  400. else
  401. begin
  402. { in widestrings S[1] is pwchar(S)[0] !! }
  403. dec(p^.location.reference.offset,2);
  404. exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,
  405. 1,p^.location.reference.base)));
  406. end;
  407. { we've also to keep left up-to-date, because it is used }
  408. { if a constant array index occurs, subject to change (FK) }
  409. set_location(p^.left^.location,p^.location);
  410. end
  411. else
  412. set_location(p^.location,p^.left^.location);
  413. { offset can only differ from 0 if arraydef }
  414. if p^.left^.resulttype^.deftype=arraydef then
  415. dec(p^.location.reference.offset,
  416. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  417. if p^.right^.treetype=ordconstn then
  418. begin
  419. { offset can only differ from 0 if arraydef }
  420. if (p^.left^.resulttype^.deftype=arraydef) then
  421. begin
  422. if not(is_open_array(p^.left^.resulttype)) then
  423. begin
  424. if (p^.right^.value>parraydef(p^.left^.resulttype)^.highrange) or
  425. (p^.right^.value<parraydef(p^.left^.resulttype)^.lowrange) then
  426. begin
  427. if (cs_check_range in aktlocalswitches) then
  428. CGMessage(parser_e_range_check_error)
  429. else
  430. CGMessage(parser_w_range_check_error);
  431. end;
  432. dec(p^.left^.location.reference.offset,
  433. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  434. end
  435. else
  436. begin
  437. { range checking for open arrays !!!! }
  438. {!!!!!!!!!!!!!!!!!}
  439. end;
  440. end
  441. else if (p^.left^.resulttype^.deftype=stringdef) then
  442. begin
  443. if (p^.right^.value=0) and not(is_shortstring(p^.left^.resulttype)) then
  444. CGMessage(cg_e_can_access_element_zero);
  445. if (cs_check_range in aktlocalswitches) then
  446. case pstringdef(p^.left^.resulttype)^.string_typ of
  447. { it's the same for ansi- and wide strings }
  448. st_widestring,
  449. st_ansistring:
  450. begin
  451. pushusedregisters(pushed,$ff);
  452. push_int(p^.right^.value);
  453. hp:=newreference(p^.location.reference);
  454. dec(hp^.offset,7);
  455. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,hp)));
  456. emitcall('FPC_ANSISTR_RANGECHECK',true);
  457. popusedregisters(pushed);
  458. maybe_loadesi;
  459. end;
  460. st_shortstring:
  461. begin
  462. {!!!!!!!!!!!!!!!!!}
  463. end;
  464. st_longstring:
  465. begin
  466. {!!!!!!!!!!!!!!!!!}
  467. end;
  468. end;
  469. end;
  470. inc(p^.left^.location.reference.offset,
  471. get_mul_size*p^.right^.value);
  472. if p^.memseg then
  473. p^.left^.location.reference.segment:=R_FS;
  474. p^.left^.resulttype:=p^.resulttype;
  475. disposetree(p^.right);
  476. _p:=p^.left;
  477. putnode(p);
  478. p:=_p;
  479. end
  480. else
  481. begin
  482. { quick hack, to overcome Delphi 2 }
  483. if (cs_regalloc in aktglobalswitches) and
  484. (p^.left^.resulttype^.deftype=arraydef) then
  485. begin
  486. extraoffset:=0;
  487. if (p^.right^.treetype=addn) then
  488. begin
  489. if p^.right^.right^.treetype=ordconstn then
  490. begin
  491. extraoffset:=p^.right^.right^.value;
  492. t:=p^.right^.left;
  493. putnode(p^.right);
  494. putnode(p^.right^.right);
  495. p^.right:=t
  496. end
  497. else if p^.right^.left^.treetype=ordconstn then
  498. begin
  499. extraoffset:=p^.right^.left^.value;
  500. t:=p^.right^.right;
  501. putnode(p^.right);
  502. putnode(p^.right^.left);
  503. p^.right:=t
  504. end;
  505. end
  506. else if (p^.right^.treetype=subn) then
  507. begin
  508. if p^.right^.right^.treetype=ordconstn then
  509. begin
  510. extraoffset:=p^.right^.right^.value;
  511. t:=p^.right^.left;
  512. putnode(p^.right);
  513. putnode(p^.right^.right);
  514. p^.right:=t
  515. end
  516. else if p^.right^.left^.treetype=ordconstn then
  517. begin
  518. extraoffset:=p^.right^.left^.value;
  519. t:=p^.right^.right;
  520. putnode(p^.right);
  521. putnode(p^.right^.left);
  522. p^.right:=t
  523. end;
  524. end;
  525. inc(p^.location.reference.offset,
  526. get_mul_size*extraoffset);
  527. end;
  528. { calculate from left to right }
  529. if (p^.location.loc<>LOC_REFERENCE) and
  530. (p^.location.loc<>LOC_MEM) then
  531. CGMessage(cg_e_illegal_expression);
  532. is_pushed:=maybe_push(p^.right^.registers32,p);
  533. secondpass(p^.right);
  534. if is_pushed then restore(p);
  535. case p^.right^.location.loc of
  536. LOC_REGISTER:
  537. begin
  538. ind:=p^.right^.location.register;
  539. case p^.right^.resulttype^.size of
  540. 1:
  541. begin
  542. hr:=reg8toreg32(ind);
  543. emit_reg_reg(A_MOVZX,S_BL,ind,hr);
  544. ind:=hr;
  545. end;
  546. 2:
  547. begin
  548. hr:=reg16toreg32(ind);
  549. emit_reg_reg(A_MOVZX,S_WL,ind,hr);
  550. ind:=hr;
  551. end;
  552. end;
  553. end;
  554. LOC_CREGISTER:
  555. begin
  556. ind:=getregister32;
  557. case p^.right^.resulttype^.size of
  558. 1:
  559. emit_reg_reg(A_MOVZX,S_BL,p^.right^.location.register,ind);
  560. 2:
  561. emit_reg_reg(A_MOVZX,S_WL,p^.right^.location.register,ind);
  562. 4:
  563. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,ind);
  564. end;
  565. end;
  566. LOC_FLAGS:
  567. begin
  568. ind:=getregister32;
  569. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.right^.location.resflags],S_B,reg32toreg8(ind))));
  570. emit_reg_reg(A_MOVZX,S_BL,reg32toreg8(ind),ind);
  571. end
  572. else
  573. begin
  574. del_reference(p^.right^.location.reference);
  575. ind:=getregister32;
  576. { Booleans are stored in an 8 bit memory location, so
  577. the use of MOVL is not correct }
  578. case p^.right^.resulttype^.size of
  579. 1:
  580. tai:=new(pai386,op_ref_reg(A_MOVZX,S_BL,newreference(p^.right^.location.reference),ind));
  581. 2:
  582. tai:=new(Pai386,op_ref_reg(A_MOVZX,S_WL,newreference(p^.right^.location.reference),ind));
  583. 4:
  584. tai:=new(Pai386,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),ind));
  585. end;
  586. exprasmlist^.concat(tai);
  587. end;
  588. end;
  589. { produce possible range check code: }
  590. if cs_check_range in aktlocalswitches then
  591. begin
  592. if p^.left^.resulttype^.deftype=arraydef then
  593. begin
  594. hp:=new_reference(R_NO,0);
  595. parraydef(p^.left^.resulttype)^.genrangecheck;
  596. hp^.symbol:=stringdup(parraydef(p^.left^.resulttype)^.getrangecheckstring);
  597. exprasmlist^.concat(new(pai386,op_reg_ref(A_BOUND,S_L,ind,hp)));
  598. end
  599. else if (p^.left^.resulttype^.deftype=stringdef) then
  600. begin
  601. case pstringdef(p^.left^.resulttype)^.string_typ of
  602. { it's the same for ansi- and wide strings }
  603. st_widestring,
  604. st_ansistring:
  605. begin
  606. pushusedregisters(pushed,$ff);
  607. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,ind)));
  608. hp:=newreference(p^.location.reference);
  609. dec(hp^.offset,7);
  610. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,hp)));
  611. emitcall('FPC_ANSISTR_RANGECHECK',true);
  612. popusedregisters(pushed);
  613. maybe_loadesi;
  614. end;
  615. st_shortstring:
  616. begin
  617. {!!!!!!!!!!!!!!!!!}
  618. end;
  619. st_longstring:
  620. begin
  621. {!!!!!!!!!!!!!!!!!}
  622. end;
  623. end;
  624. end;
  625. end;
  626. if p^.location.reference.index=R_NO then
  627. begin
  628. p^.location.reference.index:=ind;
  629. calc_emit_mul;
  630. end
  631. else
  632. begin
  633. if p^.location.reference.base=R_NO then
  634. begin
  635. case p^.location.reference.scalefactor of
  636. 2 : exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,1,p^.location.reference.index)));
  637. 4 : exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,2,p^.location.reference.index)));
  638. 8 : exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,3,p^.location.reference.index)));
  639. end;
  640. calc_emit_mul;
  641. p^.location.reference.base:=p^.location.reference.index;
  642. p^.location.reference.index:=ind;
  643. end
  644. else
  645. begin
  646. exprasmlist^.concat(new(pai386,op_ref_reg(
  647. A_LEA,S_L,newreference(p^.location.reference),
  648. p^.location.reference.index)));
  649. ungetregister32(p^.location.reference.base);
  650. { the symbol offset is loaded, }
  651. { so release the symbol name and set symbol }
  652. { to nil }
  653. stringdispose(p^.location.reference.symbol);
  654. p^.location.reference.offset:=0;
  655. calc_emit_mul;
  656. p^.location.reference.base:=p^.location.reference.index;
  657. p^.location.reference.index:=ind;
  658. end;
  659. end;
  660. if p^.memseg then
  661. p^.location.reference.segment:=R_FS;
  662. end;
  663. { have to remove a temp. wide/ansistring ?
  664. c:=(s1+s2)[i]
  665. for example
  666. }
  667. if (p^.location.loc=LOC_MEM) and
  668. assigned(p^.left) and
  669. (p^.left^.resulttype^.deftype=stringdef) then
  670. begin
  671. case pstringdef(p^.left^.resulttype)^.string_typ of
  672. st_ansistring:
  673. begin
  674. del_reference(p^.location.reference);
  675. hr:=reg32toreg8(getregister32);
  676. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_B,
  677. newreference(p^.location.reference),hr)));
  678. clear_reference(p^.location.reference);
  679. p^.location.loc:=LOC_REGISTER;
  680. p^.location.register:=hr;
  681. { we can remove all temps }
  682. removetemps(exprasmlist,temptoremove);
  683. temptoremove^.clear;
  684. end;
  685. st_widestring:
  686. begin
  687. del_reference(p^.location.reference);
  688. hr:=reg32toreg16(getregister32);
  689. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,
  690. newreference(p^.location.reference),hr))); clear_reference(p^.location.reference);
  691. p^.location.loc:=LOC_REGISTER;
  692. p^.location.register:=hr;
  693. { we can remove all temps }
  694. removetemps(exprasmlist,temptoremove);
  695. temptoremove^.clear;
  696. end;
  697. end;
  698. end;
  699. end;
  700. {*****************************************************************************
  701. SecondSelfN
  702. *****************************************************************************}
  703. procedure secondselfn(var p : ptree);
  704. begin
  705. clear_reference(p^.location.reference);
  706. if (p^.resulttype^.deftype=classrefdef) or
  707. ((p^.resulttype^.deftype=objectdef)
  708. and pobjectdef(p^.resulttype)^.isclass
  709. ) then
  710. p^.location.register:=R_ESI
  711. else
  712. p^.location.reference.base:=R_ESI;
  713. end;
  714. {*****************************************************************************
  715. SecondWithN
  716. *****************************************************************************}
  717. procedure secondwith(var p : ptree);
  718. var
  719. ref : treference;
  720. symtable : psymtable;
  721. i : longint;
  722. load : boolean;
  723. begin
  724. if assigned(p^.left) then
  725. begin
  726. secondpass(p^.left);
  727. load:=true;
  728. if p^.left^.location.reference.segment<>R_DEFAULT_SEG then
  729. message(parser_e_no_with_for_variable_in_other_segments);
  730. ref.symbol:=nil;
  731. gettempofsizereference(4,ref);
  732. if (p^.left^.treetype=loadn) and
  733. (p^.left^.symtable=aktprocsym^.definition^.localst) then
  734. begin
  735. { for local class just use the local storage }
  736. ungetiftemp(ref);
  737. new(p^.pref);
  738. p^.pref^:=p^.left^.location.reference;
  739. { don't discard symbol if in main procedure }
  740. p^.left^.location.reference.symbol:=nil;
  741. load:=false;
  742. end
  743. else if (p^.left^.resulttype^.deftype=objectdef) and
  744. pobjectdef(p^.left^.resulttype)^.isclass then
  745. begin
  746. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  747. newreference(p^.left^.location.reference),R_EDI)))
  748. end
  749. else
  750. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  751. newreference(p^.left^.location.reference),R_EDI)));
  752. if load then
  753. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  754. R_EDI,newreference(ref))));
  755. del_reference(p^.left^.location.reference);
  756. { the offset relative to (%ebp) is only needed here! }
  757. symtable:=p^.withsymtable;
  758. for i:=1 to p^.tablecount do
  759. begin
  760. symtable^.datasize:=ref.offset;
  761. symtable:=symtable^.next;
  762. end;
  763. { p^.right can be optimize out !!! }
  764. if p^.right<>nil then
  765. secondpass(p^.right);
  766. { clear some stuff }
  767. if assigned(p^.pref) then
  768. dispose(p^.pref);
  769. if load then
  770. ungetiftemp(ref);
  771. end;
  772. end;
  773. end.
  774. {
  775. $Log$
  776. Revision 1.28 1999-02-04 17:16:51 peter
  777. * fixed crash with temp ansistring indexing
  778. Revision 1.27 1999/02/04 11:44:46 florian
  779. * fixed indexed access of ansistrings to temp. ansistring, i.e.
  780. c:=(s1+s2)[i], the temp is now correctly remove and the generated
  781. code is also fixed
  782. Revision 1.26 1999/02/04 10:49:41 florian
  783. + range checking for ansi- and widestrings
  784. * made it compilable with TP
  785. Revision 1.25 1999/01/21 16:40:52 pierre
  786. * fix for constructor inside with statements
  787. Revision 1.24 1999/01/19 12:05:27 pierre
  788. * bug with @procvar=procvar fiwed
  789. Revision 1.23 1998/12/30 22:15:45 peter
  790. + farpointer type
  791. * absolutesym now also stores if its far
  792. Revision 1.22 1998/12/11 00:02:55 peter
  793. + globtype,tokens,version unit splitted from globals
  794. Revision 1.21 1998/12/10 09:47:18 florian
  795. + basic operations with int64/qord (compiler with -dint64)
  796. + rtti of enumerations extended: names are now written
  797. Revision 1.20 1998/11/25 19:12:54 pierre
  798. * var:=new(pointer_type) support added
  799. Revision 1.19 1998/11/20 15:35:55 florian
  800. * problems with rtti fixed, hope it works
  801. Revision 1.18 1998/11/17 00:36:40 peter
  802. * more ansistring fixes
  803. Revision 1.17 1998/11/16 15:35:09 pierre
  804. * added error for with if different segment
  805. Revision 1.16 1998/10/21 11:44:42 florian
  806. + check for access to index 0 of long/wide/ansi strings added,
  807. gives now an error
  808. * problem with access to contant index of ansistrings fixed
  809. Revision 1.15 1998/10/12 09:49:53 florian
  810. + support of <procedure var type>:=<pointer> in delphi mode added
  811. Revision 1.14 1998/10/02 07:20:37 florian
  812. * range checking in units doesn't work if the units are smartlinked, fixed
  813. Revision 1.13 1998/09/27 10:16:23 florian
  814. * type casts pchar<->ansistring fixed
  815. * ansistring[..] calls does now an unique call
  816. Revision 1.12 1998/09/23 15:46:36 florian
  817. * problem with with and classes fixed
  818. Revision 1.11 1998/09/17 09:42:18 peter
  819. + pass_2 for cg386
  820. * Message() -> CGMessage() for pass_1/pass_2
  821. Revision 1.10 1998/09/14 10:43:52 peter
  822. * all internal RTL functions start with FPC_
  823. Revision 1.9 1998/09/03 16:03:15 florian
  824. + rtti generation
  825. * init table generation changed
  826. Revision 1.8 1998/08/23 21:04:34 florian
  827. + rtti generation for classes added
  828. + new/dispose do now also a call to INITIALIZE/FINALIZE, if necessaray
  829. Revision 1.7 1998/08/20 11:27:40 michael
  830. * Applied Peters Fix
  831. Revision 1.6 1998/08/10 14:49:49 peter
  832. + localswitches, moduleswitches, globalswitches splitting
  833. Revision 1.5 1998/07/26 21:58:58 florian
  834. + better support for switch $H
  835. + index access to ansi strings added
  836. + assigment of data (records/arrays) containing ansi strings
  837. Revision 1.4 1998/07/24 22:16:55 florian
  838. * internal error 10 together with array access fixed. I hope
  839. that's the final fix.
  840. Revision 1.3 1998/06/25 08:48:09 florian
  841. * first version of rtti support
  842. Revision 1.2 1998/06/08 13:13:35 pierre
  843. + temporary variables now in temp_gen.pas unit
  844. because it is processor independent
  845. * mppc68k.bat modified to undefine i386 and support_mmx
  846. (which are defaults for i386)
  847. Revision 1.1 1998/06/05 17:44:13 peter
  848. * splitted cgi386
  849. }