cg386mem.pas 39 KB

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