cg386cal.pas 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate i386 assembler for in call 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 cg386cal;
  19. interface
  20. { $define AnsiStrRef}
  21. uses
  22. symtable,tree;
  23. procedure secondcallparan(var p : ptree;defcoll : pparaitem;
  24. push_from_left_to_right,inlined : boolean;para_alignment,para_offset : longint);
  25. procedure secondcalln(var p : ptree);
  26. procedure secondprocinline(var p : ptree);
  27. implementation
  28. uses
  29. globtype,systems,
  30. cobjects,verbose,globals,
  31. symconst,aasm,types,
  32. {$ifdef GDB}
  33. gdb,
  34. {$endif GDB}
  35. hcodegen,temp_gen,pass_2,
  36. cpubase,cpuasm,
  37. cgai386,tgeni386,cg386ld;
  38. {*****************************************************************************
  39. SecondCallParaN
  40. *****************************************************************************}
  41. procedure secondcallparan(var p : ptree;defcoll : pparaitem;
  42. push_from_left_to_right,inlined : boolean;para_alignment,para_offset : longint);
  43. procedure maybe_push_high;
  44. begin
  45. { open array ? }
  46. { defcoll^.data can be nil for read/write }
  47. if assigned(defcoll^.paratype.def) and
  48. push_high_param(defcoll^.paratype.def) then
  49. begin
  50. if assigned(p^.hightree) then
  51. begin
  52. secondpass(p^.hightree);
  53. { this is a longint anyway ! }
  54. push_value_para(p^.hightree,inlined,para_offset,4);
  55. end
  56. else
  57. internalerror(432645);
  58. end;
  59. end;
  60. var
  61. otlabel,oflabel : pasmlabel;
  62. { temporary variables: }
  63. tempdeftype : tdeftype;
  64. r : preference;
  65. begin
  66. { set default para_alignment to target_os.stackalignment }
  67. if para_alignment=0 then
  68. para_alignment:=target_os.stackalignment;
  69. { push from left to right if specified }
  70. if push_from_left_to_right and assigned(p^.right) then
  71. secondcallparan(p^.right,pparaitem(defcoll^.next),push_from_left_to_right,
  72. inlined,para_alignment,para_offset);
  73. otlabel:=truelabel;
  74. oflabel:=falselabel;
  75. getlabel(truelabel);
  76. getlabel(falselabel);
  77. secondpass(p^.left);
  78. { filter array constructor with c styled args }
  79. if is_array_constructor(p^.left^.resulttype) and p^.left^.cargs then
  80. begin
  81. { nothing, everything is already pushed }
  82. end
  83. { in codegen.handleread.. defcoll^.data is set to nil }
  84. else if assigned(defcoll^.paratype.def) and
  85. (defcoll^.paratype.def^.deftype=formaldef) then
  86. begin
  87. { allow @var }
  88. inc(pushedparasize,4);
  89. if p^.left^.treetype=addrn then
  90. begin
  91. { always a register }
  92. if inlined then
  93. begin
  94. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  95. emit_reg_ref(A_MOV,S_L,
  96. p^.left^.location.register,r);
  97. end
  98. else
  99. emit_reg(A_PUSH,S_L,p^.left^.location.register);
  100. ungetregister32(p^.left^.location.register);
  101. end
  102. else
  103. begin
  104. if not(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  105. CGMessage(type_e_mismatch)
  106. else
  107. begin
  108. if inlined then
  109. begin
  110. {$ifndef noAllocEdi}
  111. getexplicitregister32(R_EDI);
  112. {$endif noAllocEdi}
  113. emit_ref_reg(A_LEA,S_L,
  114. newreference(p^.left^.location.reference),R_EDI);
  115. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  116. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  117. {$ifndef noAllocEdi}
  118. ungetregister32(R_EDI);
  119. {$endif noAllocEdi}
  120. end
  121. else
  122. emitpushreferenceaddr(p^.left^.location.reference);
  123. del_reference(p^.left^.location.reference);
  124. end;
  125. end;
  126. end
  127. { handle call by reference parameter }
  128. else if (defcoll^.paratyp=vs_var) then
  129. begin
  130. if (p^.left^.location.loc<>LOC_REFERENCE) then
  131. CGMessage(cg_e_var_must_be_reference);
  132. maybe_push_high;
  133. inc(pushedparasize,4);
  134. if inlined then
  135. begin
  136. {$ifndef noAllocEdi}
  137. getexplicitregister32(R_EDI);
  138. {$endif noAllocEdi}
  139. emit_ref_reg(A_LEA,S_L,
  140. newreference(p^.left^.location.reference),R_EDI);
  141. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  142. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  143. {$ifndef noAllocEdi}
  144. ungetregister32(R_EDI);
  145. {$endif noAllocEdi}
  146. end
  147. else
  148. emitpushreferenceaddr(p^.left^.location.reference);
  149. del_reference(p^.left^.location.reference);
  150. end
  151. else
  152. begin
  153. tempdeftype:=p^.resulttype^.deftype;
  154. if tempdeftype=filedef then
  155. CGMessage(cg_e_file_must_call_by_reference);
  156. { open array must always push the address, this is needed to
  157. also push addr of small arrays (PFV) }
  158. if (assigned(defcoll^.paratype.def) and
  159. is_open_array(defcoll^.paratype.def)) or
  160. push_addr_param(p^.resulttype) then
  161. begin
  162. maybe_push_high;
  163. inc(pushedparasize,4);
  164. if inlined then
  165. begin
  166. {$ifndef noAllocEdi}
  167. getexplicitregister32(R_EDI);
  168. {$endif noAllocEdi}
  169. emit_ref_reg(A_LEA,S_L,
  170. newreference(p^.left^.location.reference),R_EDI);
  171. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  172. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  173. {$ifndef noAllocEdi}
  174. ungetregister32(R_EDI);
  175. {$endif noAllocEdi}
  176. end
  177. else
  178. emitpushreferenceaddr(p^.left^.location.reference);
  179. del_reference(p^.left^.location.reference);
  180. end
  181. else
  182. begin
  183. push_value_para(p^.left,inlined,para_offset,para_alignment);
  184. end;
  185. end;
  186. truelabel:=otlabel;
  187. falselabel:=oflabel;
  188. { push from right to left }
  189. if not push_from_left_to_right and assigned(p^.right) then
  190. secondcallparan(p^.right,pparaitem(defcoll^.next),push_from_left_to_right,
  191. inlined,para_alignment,para_offset);
  192. end;
  193. {*****************************************************************************
  194. SecondCallN
  195. *****************************************************************************}
  196. procedure secondcalln(var p : ptree);
  197. var
  198. unusedregisters : tregisterset;
  199. usablecount : byte;
  200. pushed : tpushed;
  201. hr,funcretref : treference;
  202. hregister,hregister2 : tregister;
  203. oldpushedparasize : longint;
  204. { true if ESI must be loaded again after the subroutine }
  205. loadesi : boolean;
  206. { true if a virtual method must be called directly }
  207. no_virtual_call : boolean;
  208. { true if we produce a con- or destrutor in a call }
  209. is_con_or_destructor : boolean;
  210. { true if a constructor is called again }
  211. extended_new : boolean;
  212. { adress returned from an I/O-error }
  213. iolabel : pasmlabel;
  214. { lexlevel count }
  215. i : longint;
  216. { help reference pointer }
  217. r : preference;
  218. hp,
  219. pp,params : ptree;
  220. inlined : boolean;
  221. inlinecode : ptree;
  222. para_alignment,
  223. para_offset : longint;
  224. { instruction for alignement correction }
  225. { corr : paicpu;}
  226. { we must pop this size also after !! }
  227. { must_pop : boolean; }
  228. pop_size : longint;
  229. pop_allowed : boolean;
  230. label
  231. dont_call;
  232. begin
  233. reset_reference(p^.location.reference);
  234. extended_new:=false;
  235. iolabel:=nil;
  236. inlinecode:=nil;
  237. inlined:=false;
  238. loadesi:=true;
  239. no_virtual_call:=false;
  240. unusedregisters:=unused;
  241. usablecount:=usablereg32;
  242. if (pocall_cdecl in p^.procdefinition^.proccalloptions) or
  243. (pocall_stdcall in p^.procdefinition^.proccalloptions) then
  244. para_alignment:=4
  245. else
  246. para_alignment:=target_os.stackalignment;
  247. if not assigned(p^.procdefinition) then
  248. exit;
  249. if (pocall_inline in p^.procdefinition^.proccalloptions) then
  250. begin
  251. inlined:=true;
  252. inlinecode:=p^.right;
  253. { set it to the same lexical level as the local symtable, becuase
  254. the para's are stored there }
  255. pprocdef(p^.procdefinition)^.parast^.symtablelevel:=aktprocsym^.definition^.localst^.symtablelevel;
  256. if assigned(p^.left) then
  257. inlinecode^.para_offset:=gettempofsizepersistant(inlinecode^.para_size);
  258. pprocdef(p^.procdefinition)^.parast^.address_fixup:=inlinecode^.para_offset;
  259. {$ifdef extdebug}
  260. Comment(V_debug,
  261. 'inlined parasymtable is at offset '
  262. +tostr(pprocdef(p^.procdefinition)^.parast^.address_fixup));
  263. exprasmlist^.concat(new(pai_asm_comment,init(
  264. strpnew('inlined parasymtable is at offset '
  265. +tostr(pprocdef(p^.procdefinition)^.parast^.address_fixup)))));
  266. {$endif extdebug}
  267. p^.right:=nil;
  268. { disable further inlining of the same proc
  269. in the args }
  270. {$ifdef INCLUDEOK}
  271. exclude(p^.procdefinition^.proccalloptions,pocall_inline);
  272. {$else}
  273. p^.procdefinition^.proccalloptions:=p^.procdefinition^.proccalloptions-[pocall_inline];
  274. {$endif}
  275. end;
  276. { only if no proc var }
  277. if not(assigned(p^.right)) then
  278. is_con_or_destructor:=(p^.procdefinition^.proctypeoption in [potype_constructor,potype_destructor]);
  279. { proc variables destroy all registers }
  280. if (p^.right=nil) and
  281. { virtual methods too }
  282. not(po_virtualmethod in p^.procdefinition^.procoptions) then
  283. begin
  284. if (cs_check_io in aktlocalswitches) and
  285. (po_iocheck in p^.procdefinition^.procoptions) and
  286. not(po_iocheck in aktprocsym^.definition^.procoptions) then
  287. begin
  288. getlabel(iolabel);
  289. emitlab(iolabel);
  290. end
  291. else
  292. iolabel:=nil;
  293. { save all used registers }
  294. pushusedregisters(pushed,pprocdef(p^.procdefinition)^.usedregisters);
  295. { give used registers through }
  296. usedinproc:=usedinproc or pprocdef(p^.procdefinition)^.usedregisters;
  297. end
  298. else
  299. begin
  300. pushusedregisters(pushed,$ff);
  301. usedinproc:=$ff;
  302. { no IO check for methods and procedure variables }
  303. iolabel:=nil;
  304. end;
  305. { generate the code for the parameter and push them }
  306. oldpushedparasize:=pushedparasize;
  307. pushedparasize:=0;
  308. pop_size:=0;
  309. { no inc esp for inlined procedure
  310. and for objects constructors PM }
  311. if inlined or
  312. ((p^.right=nil) and
  313. (p^.procdefinition^.proctypeoption=potype_constructor) and
  314. { quick'n'dirty check if it is a class or an object }
  315. (p^.resulttype^.deftype=orddef)) then
  316. pop_allowed:=false
  317. else
  318. pop_allowed:=true;
  319. if pop_allowed then
  320. begin
  321. { Old pushedsize aligned on 4 ? }
  322. i:=oldpushedparasize and 3;
  323. if i>0 then
  324. inc(pop_size,4-i);
  325. { This parasize aligned on 4 ? }
  326. i:=p^.procdefinition^.para_size(para_alignment) and 3;
  327. if i>0 then
  328. inc(pop_size,4-i);
  329. { insert the opcode and update pushedparasize }
  330. { never push 4 or more !! }
  331. pop_size:=pop_size mod 4;
  332. if pop_size>0 then
  333. begin
  334. inc(pushedparasize,pop_size);
  335. emit_const_reg(A_SUB,S_L,pop_size,R_ESP);
  336. {$ifdef GDB}
  337. if (cs_debuginfo in aktmoduleswitches) and
  338. (exprasmlist^.first=exprasmlist^.last) then
  339. exprasmlist^.concat(new(pai_force_line,init));
  340. {$endif GDB}
  341. end;
  342. end;
  343. if (p^.resulttype<>pdef(voiddef)) and
  344. ret_in_param(p^.resulttype) then
  345. begin
  346. funcretref.symbol:=nil;
  347. {$ifdef test_dest_loc}
  348. if dest_loc_known and (dest_loc_tree=p) and
  349. (dest_loc.loc in [LOC_REFERENCE,LOC_MEM]) then
  350. begin
  351. funcretref:=dest_loc.reference;
  352. if assigned(dest_loc.reference.symbol) then
  353. funcretref.symbol:=stringdup(dest_loc.reference.symbol^);
  354. in_dest_loc:=true;
  355. end
  356. else
  357. {$endif test_dest_loc}
  358. if inlined then
  359. begin
  360. reset_reference(funcretref);
  361. funcretref.offset:=gettempofsizepersistant(p^.procdefinition^.rettype.def^.size);
  362. funcretref.base:=procinfo^.framepointer;
  363. end
  364. else
  365. gettempofsizereference(p^.procdefinition^.rettype.def^.size,funcretref);
  366. end;
  367. if assigned(p^.left) then
  368. begin
  369. { be found elsewhere }
  370. if inlined then
  371. para_offset:=pprocdef(p^.procdefinition)^.parast^.address_fixup+
  372. pprocdef(p^.procdefinition)^.parast^.datasize
  373. else
  374. para_offset:=0;
  375. if assigned(p^.right) then
  376. secondcallparan(p^.left,pparaitem(pabstractprocdef(p^.right^.resulttype)^.para^.first),
  377. (pocall_leftright in p^.procdefinition^.proccalloptions),
  378. inlined,para_alignment,para_offset)
  379. else
  380. secondcallparan(p^.left,pparaitem(p^.procdefinition^.para^.first),
  381. (pocall_leftright in p^.procdefinition^.proccalloptions),
  382. inlined,para_alignment,para_offset);
  383. end;
  384. params:=p^.left;
  385. p^.left:=nil;
  386. if inlined then
  387. inlinecode^.retoffset:=gettempofsizepersistant(4);
  388. if ret_in_param(p^.resulttype) then
  389. begin
  390. { This must not be counted for C code
  391. complex return address is removed from stack
  392. by function itself ! }
  393. {$ifdef OLD_C_STACK}
  394. inc(pushedparasize,4); { lets try without it PM }
  395. {$endif not OLD_C_STACK}
  396. if inlined then
  397. begin
  398. {$ifndef noAllocEdi}
  399. getexplicitregister32(R_EDI);
  400. {$endif noAllocEdi}
  401. emit_ref_reg(A_LEA,S_L,
  402. newreference(funcretref),R_EDI);
  403. r:=new_reference(procinfo^.framepointer,inlinecode^.retoffset);
  404. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  405. {$ifndef noAllocEdi}
  406. ungetregister32(R_EDI);
  407. {$endif noAllocEdi}
  408. end
  409. else
  410. emitpushreferenceaddr(funcretref);
  411. end;
  412. { procedure variable ? }
  413. if (p^.right=nil) then
  414. begin
  415. { overloaded operator have no symtable }
  416. { push self }
  417. if assigned(p^.symtable) and
  418. (p^.symtable^.symtabletype=withsymtable) then
  419. begin
  420. { dirty trick to avoid the secondcall below }
  421. p^.methodpointer:=genzeronode(callparan);
  422. p^.methodpointer^.location.loc:=LOC_REGISTER;
  423. {$ifndef noAllocEDI}
  424. getexplicitregister32(R_ESI);
  425. {$endif noAllocEDI}
  426. p^.methodpointer^.location.register:=R_ESI;
  427. { ARGHHH this is wrong !!!
  428. if we can init from base class for a child
  429. class that the wrong VMT will be
  430. transfered to constructor !! }
  431. p^.methodpointer^.resulttype:=
  432. ptree(pwithsymtable(p^.symtable)^.withnode)^.left^.resulttype;
  433. { change dispose type !! }
  434. p^.disposetyp:=dt_mbleft_and_method;
  435. { make a reference }
  436. new(r);
  437. reset_reference(r^);
  438. { if assigned(ptree(pwithsymtable(p^.symtable)^.withnode)^.pref) then
  439. begin
  440. r^:=ptree(pwithsymtable(p^.symtable)^.withnode)^.pref^;
  441. end
  442. else
  443. begin
  444. r^.offset:=p^.symtable^.datasize;
  445. r^.base:=procinfo^.framepointer;
  446. end; }
  447. r^:=ptree(pwithsymtable(p^.symtable)^.withnode)^.withreference^;
  448. if (not pwithsymtable(p^.symtable)^.direct_with) or
  449. pobjectdef(p^.methodpointer^.resulttype)^.is_class then
  450. emit_ref_reg(A_MOV,S_L,r,R_ESI)
  451. else
  452. emit_ref_reg(A_LEA,S_L,r,R_ESI);
  453. end;
  454. { push self }
  455. if assigned(p^.symtable) and
  456. ((p^.symtable^.symtabletype=objectsymtable) or
  457. (p^.symtable^.symtabletype=withsymtable)) then
  458. begin
  459. if assigned(p^.methodpointer) then
  460. begin
  461. {
  462. if p^.methodpointer^.resulttype=classrefdef then
  463. begin
  464. two possibilities:
  465. 1. constructor
  466. 2. class method
  467. end
  468. else }
  469. begin
  470. case p^.methodpointer^.treetype of
  471. typen:
  472. begin
  473. { direct call to inherited method }
  474. if (po_abstractmethod in p^.procdefinition^.procoptions) then
  475. begin
  476. CGMessage(cg_e_cant_call_abstract_method);
  477. goto dont_call;
  478. end;
  479. { generate no virtual call }
  480. no_virtual_call:=true;
  481. if (sp_static in p^.symtableprocentry^.symoptions) then
  482. begin
  483. { well lets put the VMT address directly into ESI }
  484. { it is kind of dirty but that is the simplest }
  485. { way to accept virtual static functions (PM) }
  486. loadesi:=true;
  487. { if no VMT just use $0 bug0214 PM }
  488. {$ifndef noAllocEDI}
  489. getexplicitregister32(R_ESI);
  490. {$endif noAllocEDI}
  491. if not(oo_has_vmt in pobjectdef(p^.methodpointer^.resulttype)^.objectoptions) then
  492. emit_const_reg(A_MOV,S_L,0,R_ESI)
  493. else
  494. begin
  495. emit_sym_ofs_reg(A_MOV,S_L,
  496. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname),
  497. 0,R_ESI);
  498. end;
  499. { emit_reg(A_PUSH,S_L,R_ESI);
  500. this is done below !! }
  501. end
  502. else
  503. { this is a member call, so ESI isn't modfied }
  504. loadesi:=false;
  505. { a class destructor needs a flag }
  506. if pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  507. assigned(aktprocsym) and
  508. (aktprocsym^.definition^.proctypeoption=potype_destructor) then
  509. begin
  510. push_int(0);
  511. emit_reg(A_PUSH,S_L,R_ESI);
  512. end;
  513. if not(is_con_or_destructor and
  514. pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  515. assigned(aktprocsym) and
  516. (aktprocsym^.definition^.proctypeoption in [potype_constructor,potype_destructor])
  517. ) then
  518. emit_reg(A_PUSH,S_L,R_ESI);
  519. { if an inherited con- or destructor should be }
  520. { called in a con- or destructor then a warning }
  521. { will be made }
  522. { con- and destructors need a pointer to the vmt }
  523. if is_con_or_destructor and
  524. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) and
  525. assigned(aktprocsym) then
  526. begin
  527. if not(aktprocsym^.definition^.proctypeoption in
  528. [potype_constructor,potype_destructor]) then
  529. CGMessage(cg_w_member_cd_call_from_method);
  530. end;
  531. { class destructors get there flag above }
  532. { constructor flags ? }
  533. if is_con_or_destructor and
  534. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  535. assigned(aktprocsym) and
  536. (aktprocsym^.definition^.proctypeoption=potype_destructor)) then
  537. begin
  538. { a constructor needs also a flag }
  539. if pobjectdef(p^.methodpointer^.resulttype)^.is_class then
  540. push_int(0);
  541. push_int(0);
  542. end;
  543. end;
  544. hnewn:
  545. begin
  546. { extended syntax of new }
  547. { ESI must be zero }
  548. {$ifndef noAllocEDI}
  549. getexplicitregister32(R_ESI);
  550. {$endif noAllocEDI}
  551. emit_reg_reg(A_XOR,S_L,R_ESI,R_ESI);
  552. emit_reg(A_PUSH,S_L,R_ESI);
  553. { insert the vmt }
  554. emit_sym(A_PUSH,S_L,
  555. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  556. extended_new:=true;
  557. end;
  558. hdisposen:
  559. begin
  560. secondpass(p^.methodpointer);
  561. { destructor with extended syntax called from dispose }
  562. { hdisposen always deliver LOC_REFERENCE }
  563. {$ifndef noAllocEDI}
  564. getexplicitregister32(R_ESI);
  565. {$endif noAllocEDI}
  566. emit_ref_reg(A_LEA,S_L,
  567. newreference(p^.methodpointer^.location.reference),R_ESI);
  568. del_reference(p^.methodpointer^.location.reference);
  569. emit_reg(A_PUSH,S_L,R_ESI);
  570. emit_sym(A_PUSH,S_L,
  571. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  572. end;
  573. else
  574. begin
  575. { call to an instance member }
  576. if (p^.symtable^.symtabletype<>withsymtable) then
  577. begin
  578. secondpass(p^.methodpointer);
  579. {$ifndef noAllocEDI}
  580. getexplicitregister32(R_ESI);
  581. {$endif noAllocEDI}
  582. case p^.methodpointer^.location.loc of
  583. LOC_CREGISTER,
  584. LOC_REGISTER:
  585. begin
  586. emit_reg_reg(A_MOV,S_L,p^.methodpointer^.location.register,R_ESI);
  587. ungetregister32(p^.methodpointer^.location.register);
  588. end;
  589. else
  590. begin
  591. if (p^.methodpointer^.resulttype^.deftype=classrefdef) or
  592. ((p^.methodpointer^.resulttype^.deftype=objectdef) and
  593. pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  594. emit_ref_reg(A_MOV,S_L,
  595. newreference(p^.methodpointer^.location.reference),R_ESI)
  596. else
  597. emit_ref_reg(A_LEA,S_L,
  598. newreference(p^.methodpointer^.location.reference),R_ESI);
  599. del_reference(p^.methodpointer^.location.reference);
  600. end;
  601. end;
  602. end;
  603. { when calling a class method, we have to load ESI with the VMT !
  604. But, not for a class method via self }
  605. if not(po_containsself in p^.procdefinition^.procoptions) then
  606. begin
  607. if (po_classmethod in p^.procdefinition^.procoptions) and
  608. not(p^.methodpointer^.resulttype^.deftype=classrefdef) then
  609. begin
  610. { class method needs current VMT }
  611. getexplicitregister32(R_ESI);
  612. new(r);
  613. reset_reference(r^);
  614. r^.base:=R_ESI;
  615. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  616. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  617. end;
  618. { direct call to destructor: remove data }
  619. if (p^.procdefinition^.proctypeoption=potype_destructor) and
  620. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  621. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  622. emit_const(A_PUSH,S_L,1);
  623. { direct call to class constructor, don't allocate memory }
  624. if (p^.procdefinition^.proctypeoption=potype_constructor) and
  625. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  626. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  627. begin
  628. emit_const(A_PUSH,S_L,0);
  629. emit_const(A_PUSH,S_L,0);
  630. end
  631. else
  632. begin
  633. { constructor call via classreference => allocate memory }
  634. if (p^.procdefinition^.proctypeoption=potype_constructor) and
  635. (p^.methodpointer^.resulttype^.deftype=classrefdef) and
  636. (pobjectdef(pclassrefdef(p^.methodpointer^.resulttype)^.pointertype.def)^.is_class) then
  637. emit_const(A_PUSH,S_L,1);
  638. emit_reg(A_PUSH,S_L,R_ESI);
  639. end;
  640. end;
  641. if is_con_or_destructor then
  642. begin
  643. { classes don't get a VMT pointer pushed }
  644. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  645. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  646. begin
  647. if (p^.procdefinition^.proctypeoption=potype_constructor) then
  648. begin
  649. { it's no bad idea, to insert the VMT }
  650. emit_sym(A_PUSH,S_L,newasmsymbol(
  651. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  652. end
  653. { destructors haven't to dispose the instance, if this is }
  654. { a direct call }
  655. else
  656. push_int(0);
  657. end;
  658. end;
  659. end;
  660. end;
  661. end;
  662. end
  663. else
  664. begin
  665. if (po_classmethod in p^.procdefinition^.procoptions) and
  666. not(
  667. assigned(aktprocsym) and
  668. (po_classmethod in aktprocsym^.definition^.procoptions)
  669. ) then
  670. begin
  671. { class method needs current VMT }
  672. getexplicitregister32(R_ESI);
  673. new(r);
  674. reset_reference(r^);
  675. r^.base:=R_ESI;
  676. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  677. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  678. end
  679. else
  680. begin
  681. { member call, ESI isn't modified }
  682. loadesi:=false;
  683. end;
  684. { direct call to destructor: don't remove data! }
  685. if procinfo^._class^.is_class then
  686. begin
  687. if (p^.procdefinition^.proctypeoption=potype_destructor) then
  688. begin
  689. emit_const(A_PUSH,S_L,0);
  690. emit_reg(A_PUSH,S_L,R_ESI);
  691. end
  692. else if (p^.procdefinition^.proctypeoption=potype_constructor) then
  693. begin
  694. emit_const(A_PUSH,S_L,0);
  695. emit_const(A_PUSH,S_L,0);
  696. end
  697. else
  698. emit_reg(A_PUSH,S_L,R_ESI);
  699. end
  700. else
  701. begin
  702. emit_reg(A_PUSH,S_L,R_ESI);
  703. if is_con_or_destructor then
  704. begin
  705. if (p^.procdefinition^.proctypeoption=potype_constructor) then
  706. begin
  707. { it's no bad idea, to insert the VMT }
  708. emit_sym(A_PUSH,S_L,newasmsymbol(
  709. procinfo^._class^.vmt_mangledname));
  710. end
  711. { destructors haven't to dispose the instance, if this is }
  712. { a direct call }
  713. else
  714. push_int(0);
  715. end;
  716. end;
  717. end;
  718. end;
  719. { push base pointer ?}
  720. if (lexlevel>=normal_function_level) and assigned(pprocdef(p^.procdefinition)^.parast) and
  721. ((pprocdef(p^.procdefinition)^.parast^.symtablelevel)>normal_function_level) then
  722. begin
  723. { if we call a nested function in a method, we must }
  724. { push also SELF! }
  725. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  726. { access }
  727. {
  728. begin
  729. loadesi:=false;
  730. emit_reg(A_PUSH,S_L,R_ESI);
  731. end;
  732. }
  733. if lexlevel=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  734. begin
  735. new(r);
  736. reset_reference(r^);
  737. r^.offset:=procinfo^.framepointer_offset;
  738. r^.base:=procinfo^.framepointer;
  739. emit_ref(A_PUSH,S_L,r)
  740. end
  741. { this is only true if the difference is one !!
  742. but it cannot be more !! }
  743. else if (lexlevel=pprocdef(p^.procdefinition)^.parast^.symtablelevel-1) then
  744. begin
  745. emit_reg(A_PUSH,S_L,procinfo^.framepointer)
  746. end
  747. else if (lexlevel>pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  748. begin
  749. hregister:=getregister32;
  750. new(r);
  751. reset_reference(r^);
  752. r^.offset:=procinfo^.framepointer_offset;
  753. r^.base:=procinfo^.framepointer;
  754. emit_ref_reg(A_MOV,S_L,r,hregister);
  755. for i:=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) to lexlevel-1 do
  756. begin
  757. new(r);
  758. reset_reference(r^);
  759. {we should get the correct frame_pointer_offset at each level
  760. how can we do this !!! }
  761. r^.offset:=procinfo^.framepointer_offset;
  762. r^.base:=hregister;
  763. emit_ref_reg(A_MOV,S_L,r,hregister);
  764. end;
  765. emit_reg(A_PUSH,S_L,hregister);
  766. ungetregister32(hregister);
  767. end
  768. else
  769. internalerror(25000);
  770. end;
  771. if (po_virtualmethod in p^.procdefinition^.procoptions) and
  772. not(no_virtual_call) then
  773. begin
  774. { static functions contain the vmt_address in ESI }
  775. { also class methods }
  776. { Here it is quite tricky because it also depends }
  777. { on the methodpointer PM }
  778. getexplicitregister32(R_ESI);
  779. if assigned(aktprocsym) then
  780. begin
  781. if (((sp_static in aktprocsym^.symoptions) or
  782. (po_classmethod in aktprocsym^.definition^.procoptions)) and
  783. ((p^.methodpointer=nil) or (p^.methodpointer^.treetype=typen)))
  784. or
  785. (po_staticmethod in p^.procdefinition^.procoptions) or
  786. ((p^.procdefinition^.proctypeoption=potype_constructor) and
  787. { if we call a constructor from anther constructor }
  788. { esi contains self }
  789. (assigned(p^.methodpointer))) or
  790. { ESI is loaded earlier }
  791. (po_classmethod in p^.procdefinition^.procoptions) then
  792. begin
  793. new(r);
  794. reset_reference(r^);
  795. r^.base:=R_ESI;
  796. end
  797. else
  798. begin
  799. new(r);
  800. reset_reference(r^);
  801. r^.base:=R_ESI;
  802. { this is one point where we need vmt_offset (PM) }
  803. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  804. {$ifndef noAllocEdi}
  805. getexplicitregister32(R_EDI);
  806. {$endif noAllocEdi}
  807. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  808. new(r);
  809. reset_reference(r^);
  810. r^.base:=R_EDI;
  811. end;
  812. end
  813. else
  814. { aktprocsym should be assigned, also in main program }
  815. internalerror(12345);
  816. {
  817. begin
  818. new(r);
  819. reset_reference(r^);
  820. r^.base:=R_ESI;
  821. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  822. new(r);
  823. reset_reference(r^);
  824. r^.base:=R_EDI;
  825. end;
  826. }
  827. if pprocdef(p^.procdefinition)^.extnumber=-1 then
  828. internalerror(44584);
  829. r^.offset:=pprocdef(p^.procdefinition)^._class^.vmtmethodoffset(pprocdef(p^.procdefinition)^.extnumber);
  830. {$ifndef TESTOBJEXT}
  831. if (cs_check_range in aktlocalswitches) then
  832. begin
  833. emit_reg(A_PUSH,S_L,r^.base);
  834. emitcall('FPC_CHECK_OBJECT');
  835. end;
  836. {$else TESTOBJEXT}
  837. if (cs_check_range in aktlocalswitches) then
  838. begin
  839. emit_sym(A_PUSH,S_L,
  840. newasmsymbol(pprocdef(p^.procdefinition)^._class^.vmt_mangledname));
  841. emit_reg(A_PUSH,S_L,r^.base);
  842. emitcall('FPC_CHECK_OBJECT_EXT');
  843. end;
  844. {$endif TESTOBJEXT}
  845. emit_ref(A_CALL,S_NO,r);
  846. {$ifndef noAllocEdi}
  847. ungetregister32(R_EDI);
  848. {$endif noAllocEdi}
  849. end
  850. else if not inlined then
  851. emitcall(pprocdef(p^.procdefinition)^.mangledname)
  852. else { inlined proc }
  853. { inlined code is in inlinecode }
  854. begin
  855. { set poinline again }
  856. {$ifdef INCLUDEOK}
  857. include(p^.procdefinition^.proccalloptions,pocall_inline);
  858. {$else}
  859. p^.procdefinition^.proccalloptions:=p^.procdefinition^.proccalloptions+[pocall_inline];
  860. {$endif}
  861. { process the inlinecode }
  862. secondpass(inlinecode);
  863. { free the args }
  864. ungetpersistanttemp(pprocdef(p^.procdefinition)^.parast^.address_fixup);
  865. end;
  866. end
  867. else
  868. { now procedure variable case }
  869. begin
  870. secondpass(p^.right);
  871. { procedure of object? }
  872. if (po_methodpointer in p^.procdefinition^.procoptions) then
  873. begin
  874. { method pointer can't be in a register }
  875. hregister:=R_NO;
  876. { do some hacking if we call a method pointer }
  877. { which is a class member }
  878. { else ESI is overwritten ! }
  879. if (p^.right^.location.reference.base=R_ESI) or
  880. (p^.right^.location.reference.index=R_ESI) then
  881. begin
  882. del_reference(p^.right^.location.reference);
  883. {$ifndef noAllocEdi}
  884. getexplicitregister32(R_EDI);
  885. {$endif noAllocEdi}
  886. emit_ref_reg(A_MOV,S_L,
  887. newreference(p^.right^.location.reference),R_EDI);
  888. hregister:=R_EDI;
  889. end;
  890. { load self, but not if it's already explicitly pushed }
  891. if not(po_containsself in p^.procdefinition^.procoptions) then
  892. begin
  893. { load ESI }
  894. inc(p^.right^.location.reference.offset,4);
  895. getexplicitregister32(R_ESI);
  896. emit_ref_reg(A_MOV,S_L,
  897. newreference(p^.right^.location.reference),R_ESI);
  898. dec(p^.right^.location.reference.offset,4);
  899. { push self pointer }
  900. emit_reg(A_PUSH,S_L,R_ESI);
  901. end;
  902. if hregister=R_NO then
  903. emit_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))
  904. else
  905. begin
  906. {$ifndef noAllocEdi}
  907. ungetregister32(hregister);
  908. {$else noAllocEdi}
  909. { the same code, the previous line is just to }
  910. { indicate EDI actually is deallocated if allocated }
  911. { above (JM) }
  912. ungetregister32(hregister);
  913. {$endif noAllocEdi}
  914. emit_reg(A_CALL,S_NO,hregister);
  915. end;
  916. del_reference(p^.right^.location.reference);
  917. end
  918. else
  919. begin
  920. case p^.right^.location.loc of
  921. LOC_REGISTER,LOC_CREGISTER:
  922. begin
  923. emit_reg(A_CALL,S_NO,p^.right^.location.register);
  924. ungetregister32(p^.right^.location.register);
  925. end
  926. else
  927. emit_ref(A_CALL,S_NO,newreference(p^.right^.location.reference));
  928. del_reference(p^.right^.location.reference);
  929. end;
  930. end;
  931. end;
  932. { this was only for normal functions
  933. displaced here so we also get
  934. it to work for procvars PM }
  935. if (not inlined) and (pocall_clearstack in p^.procdefinition^.proccalloptions) then
  936. begin
  937. { we also add the pop_size which is included in pushedparasize }
  938. pop_size:=0;
  939. { better than an add on all processors }
  940. if pushedparasize=4 then
  941. begin
  942. {$ifndef noAllocEdi}
  943. getexplicitregister32(R_EDI);
  944. {$endif noAllocEdi}
  945. emit_reg(A_POP,S_L,R_EDI);
  946. {$ifndef noAllocEdi}
  947. ungetregister32(R_EDI);
  948. {$endif noAllocEdi}
  949. end
  950. { the pentium has two pipes and pop reg is pairable }
  951. { but the registers must be different! }
  952. else if (pushedparasize=8) and
  953. not(cs_littlesize in aktglobalswitches) and
  954. (aktoptprocessor=ClassP5) and
  955. (procinfo^._class=nil) then
  956. begin
  957. {$ifndef noAllocEdi}
  958. getexplicitregister32(R_EDI);
  959. {$endif noAllocEdi}
  960. emit_reg(A_POP,S_L,R_EDI);
  961. {$ifndef noAllocEdi}
  962. ungetregister32(R_EDI);
  963. {$endif noAllocEdi}
  964. {$ifndef noAllocEdi}
  965. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  966. {$endif noAllocEdi}
  967. emit_reg(A_POP,S_L,R_ESI);
  968. {$ifndef noAllocEdi}
  969. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  970. {$endif noAllocEdi}
  971. end
  972. else if pushedparasize<>0 then
  973. emit_const_reg(A_ADD,S_L,pushedparasize,R_ESP);
  974. end;
  975. dont_call:
  976. pushedparasize:=oldpushedparasize;
  977. unused:=unusedregisters;
  978. usablereg32:=usablecount;
  979. {$ifdef TEMPREGDEBUG}
  980. testregisters32;
  981. {$endif TEMPREGDEBUG}
  982. { a constructor could be a function with boolean result }
  983. { if calling constructor called fail we
  984. must jump directly to quickexitlabel PM
  985. but only if it is a call of an inherited constructor }
  986. if (p^.right=nil) and
  987. (p^.procdefinition^.proctypeoption=potype_constructor) and
  988. assigned(p^.methodpointer) and
  989. (p^.methodpointer^.treetype=typen) and
  990. (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  991. begin
  992. emitjmp(C_Z,faillabel);
  993. end;
  994. { handle function results }
  995. { structured results are easy to handle.... }
  996. { needed also when result_no_used !! }
  997. if (p^.resulttype<>pdef(voiddef)) and ret_in_param(p^.resulttype) then
  998. begin
  999. p^.location.loc:=LOC_MEM;
  1000. p^.location.reference.symbol:=nil;
  1001. p^.location.reference:=funcretref;
  1002. end;
  1003. { we have only to handle the result if it is used, but }
  1004. { ansi/widestrings must be registered, so we can dispose them }
  1005. if (p^.resulttype<>pdef(voiddef)) and (p^.return_value_used or
  1006. is_ansistring(p^.resulttype) or is_widestring(p^.resulttype)) then
  1007. begin
  1008. { a contructor could be a function with boolean result }
  1009. if (p^.right=nil) and
  1010. (p^.procdefinition^.proctypeoption=potype_constructor) and
  1011. { quick'n'dirty check if it is a class or an object }
  1012. (p^.resulttype^.deftype=orddef) then
  1013. begin
  1014. { this fails if popsize > 0 PM }
  1015. p^.location.loc:=LOC_FLAGS;
  1016. p^.location.resflags:=F_NE;
  1017. if extended_new then
  1018. begin
  1019. {$ifdef test_dest_loc}
  1020. if dest_loc_known and (dest_loc_tree=p) then
  1021. mov_reg_to_dest(p,S_L,R_EAX)
  1022. else
  1023. {$endif test_dest_loc}
  1024. begin
  1025. hregister:=getexplicitregister32(R_EAX);
  1026. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1027. p^.location.register:=hregister;
  1028. end;
  1029. end;
  1030. end
  1031. { structed results are easy to handle.... }
  1032. else if ret_in_param(p^.resulttype) then
  1033. begin
  1034. {p^.location.loc:=LOC_MEM;
  1035. stringdispose(p^.location.reference.symbol);
  1036. p^.location.reference:=funcretref;
  1037. already done above (PM) }
  1038. end
  1039. else
  1040. begin
  1041. if (p^.resulttype^.deftype in [orddef,enumdef]) then
  1042. begin
  1043. p^.location.loc:=LOC_REGISTER;
  1044. case p^.resulttype^.size of
  1045. 4 :
  1046. begin
  1047. {$ifdef test_dest_loc}
  1048. if dest_loc_known and (dest_loc_tree=p) then
  1049. mov_reg_to_dest(p,S_L,R_EAX)
  1050. else
  1051. {$endif test_dest_loc}
  1052. begin
  1053. hregister:=getexplicitregister32(R_EAX);
  1054. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1055. p^.location.register:=hregister;
  1056. end;
  1057. end;
  1058. 1 :
  1059. begin
  1060. {$ifdef test_dest_loc}
  1061. if dest_loc_known and (dest_loc_tree=p) then
  1062. mov_reg_to_dest(p,S_B,R_AL)
  1063. else
  1064. {$endif test_dest_loc}
  1065. begin
  1066. hregister:=getexplicitregister32(R_EAX);
  1067. emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
  1068. p^.location.register:=reg32toreg8(hregister);
  1069. end;
  1070. end;
  1071. 2 :
  1072. begin
  1073. {$ifdef test_dest_loc}
  1074. if dest_loc_known and (dest_loc_tree=p) then
  1075. mov_reg_to_dest(p,S_W,R_AX)
  1076. else
  1077. {$endif test_dest_loc}
  1078. begin
  1079. hregister:=getexplicitregister32(R_EAX);
  1080. emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
  1081. p^.location.register:=reg32toreg16(hregister);
  1082. end;
  1083. end;
  1084. 8 :
  1085. begin
  1086. {$ifdef test_dest_loc}
  1087. {$error Don't know what to do here}
  1088. {$endif test_dest_loc}
  1089. hregister:=getexplicitregister32(R_EAX);
  1090. hregister2:=getexplicitregister32(R_EDX);
  1091. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1092. emit_reg_reg(A_MOV,S_L,R_EDX,hregister2);
  1093. p^.location.registerlow:=hregister;
  1094. p^.location.registerhigh:=hregister2;
  1095. end;
  1096. else internalerror(7);
  1097. end
  1098. end
  1099. else if (p^.resulttype^.deftype=floatdef) then
  1100. case pfloatdef(p^.resulttype)^.typ of
  1101. f32bit:
  1102. begin
  1103. p^.location.loc:=LOC_REGISTER;
  1104. {$ifdef test_dest_loc}
  1105. if dest_loc_known and (dest_loc_tree=p) then
  1106. mov_reg_to_dest(p,S_L,R_EAX)
  1107. else
  1108. {$endif test_dest_loc}
  1109. begin
  1110. hregister:=getexplicitregister32(R_EAX);
  1111. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1112. p^.location.register:=hregister;
  1113. end;
  1114. end;
  1115. else
  1116. begin
  1117. p^.location.loc:=LOC_FPU;
  1118. inc(fpuvaroffset);
  1119. end;
  1120. end
  1121. else if is_ansistring(p^.resulttype) or
  1122. is_widestring(p^.resulttype) then
  1123. begin
  1124. hregister:=getexplicitregister32(R_EAX);
  1125. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1126. gettempansistringreference(hr);
  1127. decrstringref(p^.resulttype,hr);
  1128. emit_reg_ref(A_MOV,S_L,hregister,
  1129. newreference(hr));
  1130. ungetregister32(hregister);
  1131. p^.location.loc:=LOC_MEM;
  1132. p^.location.reference:=hr;
  1133. end
  1134. else
  1135. begin
  1136. p^.location.loc:=LOC_REGISTER;
  1137. {$ifdef test_dest_loc}
  1138. if dest_loc_known and (dest_loc_tree=p) then
  1139. mov_reg_to_dest(p,S_L,R_EAX)
  1140. else
  1141. {$endif test_dest_loc}
  1142. begin
  1143. hregister:=getexplicitregister32(R_EAX);
  1144. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1145. p^.location.register:=hregister;
  1146. end;
  1147. end;
  1148. end;
  1149. end;
  1150. { perhaps i/o check ? }
  1151. if iolabel<>nil then
  1152. begin
  1153. emit_sym(A_PUSH,S_L,iolabel);
  1154. emitcall('FPC_IOCHECK');
  1155. end;
  1156. if pop_size>0 then
  1157. emit_const_reg(A_ADD,S_L,pop_size,R_ESP);
  1158. { restore registers }
  1159. popusedregisters(pushed);
  1160. { at last, restore instance pointer (SELF) }
  1161. if loadesi then
  1162. maybe_loadesi;
  1163. pp:=params;
  1164. while assigned(pp) do
  1165. begin
  1166. if assigned(pp^.left) then
  1167. begin
  1168. if (pp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1169. ungetiftemp(pp^.left^.location.reference);
  1170. { process also all nodes of an array of const }
  1171. if pp^.left^.treetype=arrayconstructn then
  1172. begin
  1173. if assigned(pp^.left^.left) then
  1174. begin
  1175. hp:=pp^.left;
  1176. while assigned(hp) do
  1177. begin
  1178. if (hp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1179. ungetiftemp(hp^.left^.location.reference);
  1180. hp:=hp^.right;
  1181. end;
  1182. end;
  1183. end;
  1184. end;
  1185. pp:=pp^.right;
  1186. end;
  1187. if inlined then
  1188. ungetpersistanttemp(inlinecode^.retoffset);
  1189. disposetree(params);
  1190. { from now on the result can be freed normally }
  1191. if inlined and ret_in_param(p^.resulttype) then
  1192. persistanttemptonormal(funcretref.offset);
  1193. { if return value is not used }
  1194. if (not p^.return_value_used) and (p^.resulttype<>pdef(voiddef)) then
  1195. begin
  1196. if p^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  1197. begin
  1198. { data which must be finalized ? }
  1199. if (p^.resulttype^.needs_inittable) and
  1200. ( (p^.resulttype^.deftype<>objectdef) or
  1201. not(pobjectdef(p^.resulttype)^.is_class)) then
  1202. finalize(p^.resulttype,p^.location.reference,ret_in_param(p^.resulttype));
  1203. { release unused temp }
  1204. ungetiftemp(p^.location.reference)
  1205. end
  1206. else if p^.location.loc=LOC_FPU then
  1207. begin
  1208. { release FPU stack }
  1209. emit_reg(A_FSTP,S_NO,R_ST0);
  1210. {
  1211. dec(fpuvaroffset);
  1212. do NOT decrement as the increment before
  1213. is not called for unused results PM }
  1214. end;
  1215. end;
  1216. end;
  1217. {*****************************************************************************
  1218. SecondProcInlineN
  1219. *****************************************************************************}
  1220. procedure secondprocinline(var p : ptree);
  1221. var st : psymtable;
  1222. oldprocsym : pprocsym;
  1223. para_size : longint;
  1224. oldprocinfo : pprocinfo;
  1225. oldinlining_procedure,
  1226. nostackframe,make_global : boolean;
  1227. proc_names : tstringcontainer;
  1228. inlineentrycode,inlineexitcode : paasmoutput;
  1229. oldexitlabel,oldexit2label,oldquickexitlabel:Pasmlabel;
  1230. begin
  1231. oldinlining_procedure:=inlining_procedure;
  1232. oldexitlabel:=aktexitlabel;
  1233. oldexit2label:=aktexit2label;
  1234. oldquickexitlabel:=quickexitlabel;
  1235. getlabel(aktexitlabel);
  1236. getlabel(aktexit2label);
  1237. oldprocsym:=aktprocsym;
  1238. { save old procinfo }
  1239. getmem(oldprocinfo,sizeof(tprocinfo));
  1240. move(procinfo^,oldprocinfo^,sizeof(tprocinfo));
  1241. { we're inlining a procedure }
  1242. inlining_procedure:=true;
  1243. { set the return value }
  1244. aktprocsym:=p^.inlineprocsym;
  1245. procinfo^.returntype:=aktprocsym^.definition^.rettype;
  1246. procinfo^.return_offset:=p^.retoffset;
  1247. procinfo^.para_offset:=p^.para_offset;
  1248. { arg space has been filled by the parent secondcall }
  1249. st:=aktprocsym^.definition^.localst;
  1250. { set it to the same lexical level }
  1251. st^.symtablelevel:=oldprocsym^.definition^.localst^.symtablelevel;
  1252. if st^.datasize>0 then
  1253. begin
  1254. st^.address_fixup:=gettempofsizepersistant(st^.datasize);
  1255. {$ifdef extdebug}
  1256. Comment(V_debug,'local symtable is at offset '+tostr(st^.address_fixup));
  1257. exprasmlist^.concat(new(pai_asm_comment,init(strpnew(
  1258. 'local symtable is at offset '+tostr(st^.address_fixup)))));
  1259. {$endif extdebug}
  1260. end;
  1261. {$ifdef extdebug}
  1262. exprasmlist^.concat(new(pai_asm_comment,init(strpnew('Start of inlined proc'))));
  1263. {$endif extdebug}
  1264. { takes care of local data initialization }
  1265. inlineentrycode:=new(paasmoutput,init);
  1266. inlineexitcode:=new(paasmoutput,init);
  1267. proc_names.init;
  1268. para_size:=p^.para_size;
  1269. make_global:=false; { to avoid warning }
  1270. genentrycode(inlineentrycode,proc_names,make_global,0,para_size,nostackframe,true);
  1271. exprasmlist^.concatlist(inlineentrycode);
  1272. secondpass(p^.inlinetree);
  1273. genexitcode(inlineexitcode,0,false,true);
  1274. exprasmlist^.concatlist(inlineexitcode);
  1275. {$ifdef extdebug}
  1276. exprasmlist^.concat(new(pai_asm_comment,init(strpnew('End of inlined proc'))));
  1277. {$endif extdebug}
  1278. {we can free the local data now, reset also the fixup address }
  1279. if st^.datasize>0 then
  1280. begin
  1281. ungetpersistanttemp(st^.address_fixup);
  1282. st^.address_fixup:=0;
  1283. end;
  1284. { restore procinfo }
  1285. move(oldprocinfo^,procinfo^,sizeof(tprocinfo));
  1286. freemem(oldprocinfo,sizeof(tprocinfo));
  1287. { restore }
  1288. aktprocsym:=oldprocsym;
  1289. aktexitlabel:=oldexitlabel;
  1290. aktexit2label:=oldexit2label;
  1291. quickexitlabel:=oldquickexitlabel;
  1292. inlining_procedure:=oldinlining_procedure;
  1293. end;
  1294. end.
  1295. {
  1296. $Log$
  1297. Revision 1.126 2000-02-09 18:08:33 jonas
  1298. * added regallocs for esi
  1299. Revision 1.125 2000/02/09 13:22:45 peter
  1300. * log truncated
  1301. Revision 1.124 2000/02/04 20:00:21 florian
  1302. * an exception in a construcor calls now the destructor (this applies only
  1303. to classes)
  1304. Revision 1.123 2000/01/26 15:03:59 peter
  1305. * fixed pop_size included twice with clearstack
  1306. Revision 1.122 2000/01/26 12:02:29 peter
  1307. * abstractprocdef.para_size needs alignment parameter
  1308. * secondcallparan gets para_alignment size instead of dword_align
  1309. Revision 1.121 2000/01/23 18:50:07 peter
  1310. * fixed missing push esi for constructor calling
  1311. Revision 1.120 2000/01/21 22:06:16 florian
  1312. * fixed for the fix of bug 793
  1313. * fpu variables modified by nested subroutines aren't regable anymore
  1314. * $maxfpuregisters doesn't modify anymore the behavior of a procedure before
  1315. Revision 1.119 2000/01/21 12:17:41 jonas
  1316. * regallocation fixes
  1317. Revision 1.118 2000/01/20 12:14:47 florian
  1318. * bug 793 fixed
  1319. Revision 1.117 2000/01/16 22:17:11 peter
  1320. * renamed call_offset to para_offset
  1321. Revision 1.116 2000/01/09 12:35:00 jonas
  1322. * changed edi allocation to use getexplicitregister32/ungetregister
  1323. (adapted tgeni386 a bit for this) and enabled it by default
  1324. * fixed very big and stupid bug of mine in cg386mat that broke the
  1325. include() code (and make cycle :( ) if you compiled without
  1326. -dnewoptimizations
  1327. Revision 1.115 2000/01/09 01:44:19 jonas
  1328. + (de)allocation info for EDI to fix reported bug on mailinglist.
  1329. Also some (de)allocation info for ESI added. Between -dallocEDI
  1330. because at this time of the night bugs could easily slip in ;)
  1331. Revision 1.114 2000/01/07 01:14:20 peter
  1332. * updated copyright to 2000
  1333. Revision 1.113 1999/12/22 01:01:46 peter
  1334. - removed freelabel()
  1335. * added undefined label detection in internal assembler, this prevents
  1336. a lot of ld crashes and wrong .o files
  1337. * .o files aren't written anymore if errors have occured
  1338. * inlining of assembler labels is now correct
  1339. Revision 1.112 1999/12/13 21:49:54 pierre
  1340. * bug in extdebugg code for inlined procedures
  1341. Revision 1.111 1999/11/30 10:40:42 peter
  1342. + ttype, tsymlist
  1343. Revision 1.110 1999/11/06 14:34:17 peter
  1344. * truncated log to 20 revs
  1345. Revision 1.109 1999/11/04 00:23:58 pierre
  1346. * fix for fpuvaroffset for unused return value
  1347. Revision 1.108 1999/10/26 12:30:40 peter
  1348. * const parameter is now checked
  1349. * better and generic check if a node can be used for assigning
  1350. * export fixes
  1351. * procvar equal works now (it never had worked at least from 0.99.8)
  1352. * defcoll changed to linkedlist with pparaitem so it can easily be
  1353. walked both directions
  1354. Revision 1.107 1999/10/08 15:40:47 pierre
  1355. * use and remember that C functions with complex data results use ret $4
  1356. Revision 1.106 1999/09/27 23:44:46 peter
  1357. * procinfo is now a pointer
  1358. * support for result setting in sub procedure
  1359. Revision 1.105 1999/09/26 13:26:02 florian
  1360. * exception patch of Romio nevertheless the excpetion handling
  1361. needs some corections regarding register saving
  1362. * gettempansistring is again a procedure
  1363. }