cg386mem.pas 39 KB

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